1 /* oc.c - object class routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 Avlnode *oc_cache = 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;
166 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
167 if ( oir ) return oir->oir_oc;
169 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
173 avl_insert( &oc_cache, (caddr_t) oir,
174 oc_index_cmp, avl_dup_error );
176 return( oir->oir_oc );
182 static LDAP_SLIST_HEAD(OCUList, slap_object_class) oc_undef_list
183 = LDAP_SLIST_HEAD_INITIALIZER(&oc_undef_list);
186 oc_bvfind_undef( struct berval *ocname )
188 ObjectClass *oc = oc_bvfind( ocname );
194 LDAP_SLIST_FOREACH( oc, &oc_undef_list, soc_next ) {
195 int d = oc->soc_cname.bv_len - ocname->bv_len;
201 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
210 oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
211 memset( oc, 0, sizeof( ObjectClass ) );
213 oc->soc_cname.bv_len = ocname->bv_len;
214 oc->soc_cname.bv_val = (char *)&oc[ 1 ];
215 AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
217 LDAP_SLIST_NEXT( oc, soc_next ) = NULL;
218 LDAP_SLIST_INSERT_HEAD( &oc_undef_list, oc, soc_next );
232 AttributeType **satp;
238 sat = at_find(*attrs1);
241 return SLAP_SCHERR_ATTR_NOT_FOUND;
244 if( is_at_operational( sat )) (*op)++;
246 if ( at_find_in_list(sat, soc->soc_required) < 0) {
247 if ( at_append_to_list(sat, &soc->soc_required) ) {
249 return SLAP_SCHERR_OUTOFMEM;
254 /* Now delete duplicates from the allowed list */
255 for ( satp = soc->soc_required; *satp; satp++ ) {
256 i = at_find_in_list(*satp,soc->soc_allowed);
258 at_delete_from_list(i, &soc->soc_allowed);
278 sat = at_find(*attrs1);
281 return SLAP_SCHERR_ATTR_NOT_FOUND;
284 if( is_at_operational( sat )) (*op)++;
286 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
287 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
288 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
290 return SLAP_SCHERR_OUTOFMEM;
313 if ( !soc->soc_sups ) {
314 /* We are at the first recursive level */
322 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
323 sizeof(ObjectClass *));
329 soc1 = oc_find(*sups1);
332 return SLAP_SCHERR_CLASS_NOT_FOUND;
335 /* check object class usage
336 * abstract classes can only sup abstract classes
337 * structural classes can not sup auxiliary classes
338 * auxiliary classes can not sup structural classes
340 if( soc->soc_kind != soc1->soc_kind
341 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
344 return SLAP_SCHERR_CLASS_BAD_SUP;
347 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
349 return SLAP_SCHERR_CLASS_BAD_SUP;
352 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
355 soc->soc_sups[nsups] = soc1;
358 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
359 if ( code ) return code;
361 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
362 if ( code ) return code;
364 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
365 if ( code ) return code;
380 avl_free(oc_index, ldap_memfree);
381 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
382 o = LDAP_SLIST_FIRST(&oc_list);
383 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
385 if (o->soc_sups) ldap_memfree(o->soc_sups);
386 if (o->soc_required) ldap_memfree(o->soc_required);
387 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
388 ldap_objectclass_free((LDAPObjectClass *)o);
391 while( !LDAP_SLIST_EMPTY(&oc_undef_list) ) {
392 o = LDAP_SLIST_FIRST(&oc_undef_list);
393 LDAP_SLIST_REMOVE_HEAD(&oc_undef_list, soc_next);
395 ch_free( (ObjectClass *)o );
404 struct oindexrec *oir;
407 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
408 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
410 if ( soc->soc_oid ) {
411 oir = (struct oindexrec *)
412 ch_calloc( 1, sizeof(struct oindexrec) );
413 oir->oir_name.bv_val = soc->soc_oid;
414 oir->oir_name.bv_len = strlen( soc->soc_oid );
417 assert( oir->oir_name.bv_val );
418 assert( oir->oir_oc );
420 if ( avl_insert( &oc_index, (caddr_t) oir,
421 oc_index_cmp, avl_dup_error ) )
425 return SLAP_SCHERR_CLASS_DUP;
428 /* FIX: temporal consistency check */
429 assert( oc_bvfind(&oir->oir_name) != NULL );
432 if ( (names = soc->soc_names) ) {
434 oir = (struct oindexrec *)
435 ch_calloc( 1, sizeof(struct oindexrec) );
436 oir->oir_name.bv_val = *names;
437 oir->oir_name.bv_len = strlen( *names );
440 assert( oir->oir_name.bv_val );
441 assert( oir->oir_oc );
443 if ( avl_insert( &oc_index, (caddr_t) oir,
444 oc_index_cmp, avl_dup_error ) )
448 return SLAP_SCHERR_CLASS_DUP;
451 /* FIX: temporal consistency check */
452 assert( oc_bvfind(&oir->oir_name) != NULL );
471 if ( oc->oc_names != NULL ) {
474 for( i=0; oc->oc_names[i]; i++ ) {
475 if( !slap_valid_descr( oc->oc_names[i] ) ) {
476 return SLAP_SCHERR_BAD_DESCR;
481 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
482 /* Expand OID macros */
483 char *oid = oidm_find( oc->oc_oid );
486 return SLAP_SCHERR_OIDM;
488 if ( oid != oc->oc_oid ) {
489 ldap_memfree( oc->oc_oid );
494 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
495 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
497 if( oc->oc_names != NULL ) {
498 soc->soc_cname.bv_val = soc->soc_names[0];
500 soc->soc_cname.bv_val = soc->soc_oid;
502 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
504 if( soc->soc_sup_oids == NULL &&
505 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
507 /* structural object classes implicitly inherit from 'top' */
508 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
509 code = oc_add_sups( soc, top_oids, &op, err );
511 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
514 if ( code != 0 ) return code;
515 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
517 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
518 if ( code != 0 ) return code;
520 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
521 if ( code != 0 ) return code;
523 if( user && op ) return SLAP_SCHERR_CLASS_BAD_USAGE;
525 code = oc_insert(soc,err);
530 oc_schema_info( Entry *e )
532 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
537 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
538 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
540 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
544 nval = oc->soc_cname;
547 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
548 (long) val.bv_len, val.bv_val, nval.bv_val );
551 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
554 ldap_memfree( val.bv_val );