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>
27 int is_object_subclass(
33 if( sub == NULL || sup == NULL ) return 0;
36 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
37 sup->soc_oid, sub->soc_oid, sup == sub );
44 if( sub->soc_sups == NULL ) {
48 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
49 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
57 int is_entry_objectclass(
63 * set_flags should only be true if oc is one of operational
64 * object classes which we support objectClass flags for
65 * (e.g., referral, alias, ...). See <slap.h>.
70 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
72 assert(!( e == NULL || oc == NULL ));
74 if( e == NULL || oc == NULL ) {
78 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
79 /* flags are set, use them */
80 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
84 * find objectClass attribute
86 attr = attr_find(e->e_attrs, objectClass);
88 /* no objectClass attribute */
89 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
90 "no objectClass attribute\n",
91 e->e_dn == NULL ? "" : e->e_dn,
92 oc->soc_oclass.oc_oid, 0 );
97 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
98 ObjectClass *objectClass = oc_bvfind( bv );
100 if ( !set_flags && objectClass == oc ) {
104 if ( objectClass != NULL ) {
105 e->e_ocflags |= objectClass->soc_flags;
109 /* mark flags as set */
110 e->e_ocflags |= SLAP_OC__END;
112 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
117 struct berval oir_name;
121 static Avlnode *oc_index = NULL;
122 static LDAP_SLIST_HEAD(OCList, slap_object_class) oc_list
123 = LDAP_SLIST_HEAD_INITIALIZER(&oc_list);
130 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
131 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
133 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
141 const struct berval *name = v_name;
142 const struct oindexrec *oir = v_oir;
143 int i = name->bv_len - oir->oir_name.bv_len;
145 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
149 oc_find( const char *ocname )
153 bv.bv_val = (char *)ocname;
154 bv.bv_len = strlen( ocname );
156 return( oc_bvfind( &bv ) );
160 oc_bvfind( struct berval *ocname )
162 struct oindexrec *oir;
164 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
167 return( oir->oir_oc );
182 AttributeType **satp;
188 sat = at_find(*attrs1);
191 return SLAP_SCHERR_ATTR_NOT_FOUND;
194 if( is_at_operational( sat )) (*op)++;
196 if ( at_find_in_list(sat, soc->soc_required) < 0) {
197 if ( at_append_to_list(sat, &soc->soc_required) ) {
199 return SLAP_SCHERR_OUTOFMEM;
204 /* Now delete duplicates from the allowed list */
205 for ( satp = soc->soc_required; *satp; satp++ ) {
206 i = at_find_in_list(*satp,soc->soc_allowed);
208 at_delete_from_list(i, &soc->soc_allowed);
228 sat = at_find(*attrs1);
231 return SLAP_SCHERR_ATTR_NOT_FOUND;
234 if( is_at_operational( sat )) (*op)++;
236 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
237 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
238 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
240 return SLAP_SCHERR_OUTOFMEM;
263 if ( !soc->soc_sups ) {
264 /* We are at the first recursive level */
272 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
273 sizeof(ObjectClass *));
279 soc1 = oc_find(*sups1);
282 return SLAP_SCHERR_CLASS_NOT_FOUND;
285 /* check object class usage
286 * abstract classes can only sup abstract classes
287 * structural classes can not sup auxiliary classes
288 * auxiliary classes can not sup structural classes
290 if( soc->soc_kind != soc1->soc_kind
291 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
294 return SLAP_SCHERR_CLASS_BAD_SUP;
297 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
299 return SLAP_SCHERR_CLASS_BAD_SUP;
302 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
305 soc->soc_sups[nsups] = soc1;
308 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
309 if ( code ) return code;
311 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
312 if ( code ) return code;
314 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
315 if ( code ) return code;
330 avl_free(oc_index, ldap_memfree);
331 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
332 o = LDAP_SLIST_FIRST(&oc_list);
333 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
335 if (o->soc_sups) ldap_memfree(o->soc_sups);
336 if (o->soc_required) ldap_memfree(o->soc_required);
337 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
338 ldap_objectclass_free((LDAPObjectClass *)o);
347 struct oindexrec *oir;
350 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
351 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
353 if ( soc->soc_oid ) {
354 oir = (struct oindexrec *)
355 ch_calloc( 1, sizeof(struct oindexrec) );
356 oir->oir_name.bv_val = soc->soc_oid;
357 oir->oir_name.bv_len = strlen( soc->soc_oid );
360 assert( oir->oir_name.bv_val );
361 assert( oir->oir_oc );
363 if ( avl_insert( &oc_index, (caddr_t) oir,
364 oc_index_cmp, 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 oc_index_cmp, avl_dup_error ) )
391 return SLAP_SCHERR_CLASS_DUP;
394 /* FIX: temporal consistency check */
395 assert( oc_bvfind(&oir->oir_name) != NULL );
414 if ( oc->oc_names != NULL ) {
417 for( i=0; oc->oc_names[i]; i++ ) {
418 if( !slap_valid_descr( oc->oc_names[i] ) ) {
419 return SLAP_SCHERR_BAD_DESCR;
424 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
425 /* Expand OID macros */
426 char *oid = oidm_find( oc->oc_oid );
429 return SLAP_SCHERR_OIDM;
431 if ( oid != oc->oc_oid ) {
432 ldap_memfree( oc->oc_oid );
437 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
438 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
440 if( oc->oc_names != NULL ) {
441 soc->soc_cname.bv_val = soc->soc_names[0];
443 soc->soc_cname.bv_val = soc->soc_oid;
445 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
447 if( soc->soc_sup_oids == NULL &&
448 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
450 /* structural object classes implicitly inherit from 'top' */
451 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
452 code = oc_add_sups( soc, top_oids, &op, err );
454 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
457 if ( code != 0 ) return code;
458 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
460 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
461 if ( code != 0 ) return code;
463 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
464 if ( code != 0 ) return code;
466 if( user && op ) return SLAP_SCHERR_CLASS_BAD_USAGE;
468 code = oc_insert(soc,err);
473 oc_schema_info( Entry *e )
475 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
480 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
481 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
483 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
487 nval = oc->soc_cname;
490 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
491 (long) val.bv_len, val.bv_val, nval.bv_val );
494 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
497 ldap_memfree( val.bv_val );