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 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 );
173 static LDAP_SLIST_HEAD(OCUList, slap_object_class) oc_undef_list
174 = LDAP_SLIST_HEAD_INITIALIZER(&oc_undef_list);
177 oc_bvfind_undef( struct berval *ocname )
179 ObjectClass *oc = oc_bvfind( ocname );
185 LDAP_SLIST_FOREACH( oc, &oc_undef_list, soc_next ) {
186 int d = oc->soc_cname.bv_len - ocname->bv_len;
192 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
201 oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
202 memset( oc, 0, sizeof( ObjectClass ) );
204 oc->soc_cname.bv_len = ocname->bv_len;
205 oc->soc_cname.bv_val = (char *)&oc[ 1 ];
206 AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
208 LDAP_SLIST_NEXT( oc, soc_next ) = NULL;
209 LDAP_SLIST_INSERT_HEAD( &oc_undef_list, oc, soc_next );
223 AttributeType **satp;
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 if ( at_append_to_list(sat, &soc->soc_required) ) {
240 return SLAP_SCHERR_OUTOFMEM;
245 /* Now delete duplicates from the allowed list */
246 for ( satp = soc->soc_required; *satp; satp++ ) {
247 i = at_find_in_list(*satp,soc->soc_allowed);
249 at_delete_from_list(i, &soc->soc_allowed);
269 sat = at_find(*attrs1);
272 return SLAP_SCHERR_ATTR_NOT_FOUND;
275 if( is_at_operational( sat )) (*op)++;
277 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
278 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
279 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
281 return SLAP_SCHERR_OUTOFMEM;
304 if ( !soc->soc_sups ) {
305 /* We are at the first recursive level */
313 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
314 sizeof(ObjectClass *));
320 soc1 = oc_find(*sups1);
323 return SLAP_SCHERR_CLASS_NOT_FOUND;
326 /* check object class usage
327 * abstract classes can only sup abstract classes
328 * structural classes can not sup auxiliary classes
329 * auxiliary classes can not sup structural classes
331 if( soc->soc_kind != soc1->soc_kind
332 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
335 return SLAP_SCHERR_CLASS_BAD_SUP;
338 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
340 return SLAP_SCHERR_CLASS_BAD_SUP;
343 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
346 soc->soc_sups[nsups] = soc1;
349 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
350 if ( code ) return code;
352 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
353 if ( code ) return code;
355 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
356 if ( code ) return code;
371 avl_free(oc_index, ldap_memfree);
372 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
373 o = LDAP_SLIST_FIRST(&oc_list);
374 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
376 if (o->soc_sups) ldap_memfree(o->soc_sups);
377 if (o->soc_required) ldap_memfree(o->soc_required);
378 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
379 ldap_objectclass_free((LDAPObjectClass *)o);
382 while( !LDAP_SLIST_EMPTY(&oc_undef_list) ) {
383 o = LDAP_SLIST_FIRST(&oc_undef_list);
384 LDAP_SLIST_REMOVE_HEAD(&oc_undef_list, soc_next);
386 ch_free( (ObjectClass *)o );
395 struct oindexrec *oir;
398 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
399 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
401 if ( soc->soc_oid ) {
402 oir = (struct oindexrec *)
403 ch_calloc( 1, sizeof(struct oindexrec) );
404 oir->oir_name.bv_val = soc->soc_oid;
405 oir->oir_name.bv_len = strlen( soc->soc_oid );
408 assert( oir->oir_name.bv_val );
409 assert( oir->oir_oc );
411 if ( avl_insert( &oc_index, (caddr_t) oir,
412 oc_index_cmp, avl_dup_error ) )
416 return SLAP_SCHERR_CLASS_DUP;
419 /* FIX: temporal consistency check */
420 assert( oc_bvfind(&oir->oir_name) != NULL );
423 if ( (names = soc->soc_names) ) {
425 oir = (struct oindexrec *)
426 ch_calloc( 1, sizeof(struct oindexrec) );
427 oir->oir_name.bv_val = *names;
428 oir->oir_name.bv_len = strlen( *names );
431 assert( oir->oir_name.bv_val );
432 assert( oir->oir_oc );
434 if ( avl_insert( &oc_index, (caddr_t) oir,
435 oc_index_cmp, avl_dup_error ) )
439 return SLAP_SCHERR_CLASS_DUP;
442 /* FIX: temporal consistency check */
443 assert( oc_bvfind(&oir->oir_name) != NULL );
462 if ( oc->oc_names != NULL ) {
465 for( i=0; oc->oc_names[i]; i++ ) {
466 if( !slap_valid_descr( oc->oc_names[i] ) ) {
467 return SLAP_SCHERR_BAD_DESCR;
472 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
473 /* Expand OID macros */
474 char *oid = oidm_find( oc->oc_oid );
477 return SLAP_SCHERR_OIDM;
479 if ( oid != oc->oc_oid ) {
480 ldap_memfree( oc->oc_oid );
485 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
486 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
488 if( oc->oc_names != NULL ) {
489 soc->soc_cname.bv_val = soc->soc_names[0];
491 soc->soc_cname.bv_val = soc->soc_oid;
493 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
495 if( soc->soc_sup_oids == NULL &&
496 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
498 /* structural object classes implicitly inherit from 'top' */
499 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
500 code = oc_add_sups( soc, top_oids, &op, err );
502 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
505 if ( code != 0 ) return code;
506 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
508 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
509 if ( code != 0 ) return code;
511 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
512 if ( code != 0 ) return code;
514 if( user && op ) return SLAP_SCHERR_CLASS_BAD_USAGE;
516 code = oc_insert(soc,err);
521 oc_schema_info( Entry *e )
523 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
528 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
529 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
531 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
535 nval = oc->soc_cname;
538 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
539 (long) val.bv_len, val.bv_val, nval.bv_val );
542 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
545 ldap_memfree( val.bv_val );