1 /* oc.c - object class routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
28 int is_object_subclass(
34 if( sub == NULL || sup == NULL ) return 0;
37 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
38 sup->soc_oid, sub->soc_oid, sup == sub );
45 if( sub->soc_sups == NULL ) {
49 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
50 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
58 int is_entry_objectclass(
64 * set_flags should only be true if oc is one of operational
65 * object classes which we support objectClass flags for
66 * (e.g., referral, alias, ...). See <slap.h>.
71 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
73 assert(!( e == NULL || oc == NULL ));
75 if( e == NULL || oc == NULL ) {
79 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
80 /* flags are set, use them */
81 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
85 * find objectClass attribute
87 attr = attr_find(e->e_attrs, objectClass);
89 /* no objectClass attribute */
90 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
91 "no objectClass attribute\n",
92 e->e_dn == NULL ? "" : e->e_dn,
93 oc->soc_oclass.oc_oid, 0 );
98 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
99 ObjectClass *objectClass = oc_bvfind( bv );
101 if ( !set_flags && objectClass == oc ) {
105 if ( objectClass != NULL ) {
106 e->e_ocflags |= objectClass->soc_flags;
110 /* mark flags as set */
111 e->e_ocflags |= SLAP_OC__END;
113 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
118 struct berval oir_name;
122 static Avlnode *oc_index = NULL;
123 static LDAP_SLIST_HEAD(OCList, slap_object_class) oc_list
124 = LDAP_SLIST_HEAD_INITIALIZER(&oc_list);
131 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
132 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
134 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
142 const struct berval *name = v_name;
143 const struct oindexrec *oir = v_oir;
144 int i = name->bv_len - oir->oir_name.bv_len;
146 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
150 oc_find( const char *ocname )
154 bv.bv_val = (char *)ocname;
155 bv.bv_len = strlen( ocname );
157 return( oc_bvfind( &bv ) );
161 oc_bvfind( struct berval *ocname )
163 struct oindexrec *oir;
165 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
168 return( oir->oir_oc );
183 AttributeType **satp;
189 sat = at_find(*attrs1);
192 return SLAP_SCHERR_ATTR_NOT_FOUND;
195 if( is_at_operational( sat )) (*op)++;
197 if ( at_find_in_list(sat, soc->soc_required) < 0) {
198 if ( at_append_to_list(sat, &soc->soc_required) ) {
200 return SLAP_SCHERR_OUTOFMEM;
205 /* Now delete duplicates from the allowed list */
206 for ( satp = soc->soc_required; *satp; satp++ ) {
207 i = at_find_in_list(*satp,soc->soc_allowed);
209 at_delete_from_list(i, &soc->soc_allowed);
229 sat = at_find(*attrs1);
232 return SLAP_SCHERR_ATTR_NOT_FOUND;
235 if( is_at_operational( sat )) (*op)++;
237 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
238 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
239 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
241 return SLAP_SCHERR_OUTOFMEM;
264 if ( !soc->soc_sups ) {
265 /* We are at the first recursive level */
273 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
274 sizeof(ObjectClass *));
280 soc1 = oc_find(*sups1);
283 return SLAP_SCHERR_CLASS_NOT_FOUND;
286 /* check object class usage
287 * abstract classes can only sup abstract classes
288 * structural classes can not sup auxiliary classes
289 * auxiliary classes can not sup structural classes
291 if( soc->soc_kind != soc1->soc_kind
292 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
295 return SLAP_SCHERR_CLASS_BAD_SUP;
298 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
300 return SLAP_SCHERR_CLASS_BAD_SUP;
303 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
306 soc->soc_sups[nsups] = soc1;
309 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
310 if ( code ) return code;
312 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
313 if ( code ) return code;
315 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
316 if ( code ) return code;
331 avl_free(oc_index, ldap_memfree);
332 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
333 o = LDAP_SLIST_FIRST(&oc_list);
334 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
336 if (o->soc_sups) ldap_memfree(o->soc_sups);
337 if (o->soc_required) ldap_memfree(o->soc_required);
338 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
339 ldap_objectclass_free((LDAPObjectClass *)o);
348 struct oindexrec *oir;
351 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
352 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
354 if ( soc->soc_oid ) {
355 oir = (struct oindexrec *)
356 ch_calloc( 1, sizeof(struct oindexrec) );
357 oir->oir_name.bv_val = soc->soc_oid;
358 oir->oir_name.bv_len = strlen( soc->soc_oid );
361 assert( oir->oir_name.bv_val );
362 assert( oir->oir_oc );
364 if ( avl_insert( &oc_index, (caddr_t) oir,
365 oc_index_cmp, avl_dup_error ) )
369 return SLAP_SCHERR_CLASS_DUP;
372 /* FIX: temporal consistency check */
373 assert( oc_bvfind(&oir->oir_name) != NULL );
376 if ( (names = soc->soc_names) ) {
378 oir = (struct oindexrec *)
379 ch_calloc( 1, sizeof(struct oindexrec) );
380 oir->oir_name.bv_val = *names;
381 oir->oir_name.bv_len = strlen( *names );
384 assert( oir->oir_name.bv_val );
385 assert( oir->oir_oc );
387 if ( avl_insert( &oc_index, (caddr_t) oir,
388 oc_index_cmp, avl_dup_error ) )
392 return SLAP_SCHERR_CLASS_DUP;
395 /* FIX: temporal consistency check */
396 assert( oc_bvfind(&oir->oir_name) != NULL );
415 if ( oc->oc_names != NULL ) {
418 for( i=0; oc->oc_names[i]; i++ ) {
419 if( !slap_valid_descr( oc->oc_names[i] ) ) {
420 return SLAP_SCHERR_BAD_DESCR;
425 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
426 /* Expand OID macros */
427 char *oid = oidm_find( oc->oc_oid );
430 return SLAP_SCHERR_OIDM;
432 if ( oid != oc->oc_oid ) {
433 ldap_memfree( oc->oc_oid );
438 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
439 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
441 if( oc->oc_names != NULL ) {
442 soc->soc_cname.bv_val = soc->soc_names[0];
444 soc->soc_cname.bv_val = soc->soc_oid;
446 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
448 if( soc->soc_sup_oids == NULL &&
449 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
451 /* structural object classes implicitly inherit from 'top' */
452 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
453 code = oc_add_sups( soc, top_oids, &op, err );
455 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
458 if ( code != 0 ) return code;
459 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
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_USAGE;
469 code = oc_insert(soc,err);
474 oc_schema_info( Entry *e )
476 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
481 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
482 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
484 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
488 nval = oc->soc_cname;
491 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
492 (long) val.bv_len, val.bv_val, nval.bv_val );
495 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
498 ldap_memfree( val.bv_val );