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;
38 LDAP_LOG ( OPERATION, ARGS,
39 "is_object_subclass(%s,%s) %d\n",
40 sup->soc_oid, sub->soc_oid, sup == sub );
42 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
43 sup->soc_oid, sub->soc_oid, sup == sub );
51 if( sub->soc_sups == NULL ) {
55 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
56 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
64 int is_entry_objectclass(
70 * set_flags should only be true if oc is one of operational
71 * object classes which we support objectClass flags for
72 * (e.g., referral, alias, ...). See <slap.h>.
77 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
79 assert(!( e == NULL || oc == NULL ));
81 if( e == NULL || oc == NULL ) {
85 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
86 /* flags are set, use them */
87 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
91 * find objectClass attribute
93 attr = attr_find(e->e_attrs, objectClass);
95 /* no objectClass attribute */
97 LDAP_LOG( OPERATION, ERR,
98 "is_entry_objectclass: dn(%s), oid (%s), no objectClass "
99 "attribute.\n", e->e_dn == NULL ? "" : e->e_dn,
100 oc->soc_oclass.oc_oid, 0 );
102 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
103 "no objectClass attribute\n",
104 e->e_dn == NULL ? "" : e->e_dn,
105 oc->soc_oclass.oc_oid, 0 );
111 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
112 ObjectClass *objectClass = oc_bvfind( bv );
114 if ( !set_flags && objectClass == oc ) {
118 if ( objectClass != NULL ) {
119 e->e_ocflags |= objectClass->soc_flags;
123 /* mark flags as set */
124 e->e_ocflags |= SLAP_OC__END;
126 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
131 struct berval oir_name;
135 static Avlnode *oc_index = NULL;
136 static LDAP_SLIST_HEAD(OCList, slap_object_class) oc_list
137 = LDAP_SLIST_HEAD_INITIALIZER(&oc_list);
144 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
145 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
148 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
156 const struct berval *name = v_name;
157 const struct oindexrec *oir = v_oir;
158 int i = name->bv_len - oir->oir_name.bv_len;
161 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
165 oc_find( const char *ocname )
169 bv.bv_val = (char *)ocname;
170 bv.bv_len = strlen( ocname );
172 return( oc_bvfind( &bv ) );
176 oc_bvfind( struct berval *ocname )
178 struct oindexrec *oir;
180 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
183 return( oir->oir_oc );
198 AttributeType **satp;
204 sat = at_find(*attrs1);
207 return SLAP_SCHERR_ATTR_NOT_FOUND;
210 if( is_at_operational( sat )) (*op)++;
212 if ( at_find_in_list(sat, soc->soc_required) < 0) {
213 if ( at_append_to_list(sat, &soc->soc_required) ) {
215 return SLAP_SCHERR_OUTOFMEM;
220 /* Now delete duplicates from the allowed list */
221 for ( satp = soc->soc_required; *satp; satp++ ) {
222 i = at_find_in_list(*satp,soc->soc_allowed);
224 at_delete_from_list(i, &soc->soc_allowed);
244 sat = at_find(*attrs1);
247 return SLAP_SCHERR_ATTR_NOT_FOUND;
250 if( is_at_operational( sat )) (*op)++;
252 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
253 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
254 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
256 return SLAP_SCHERR_OUTOFMEM;
279 if ( !soc->soc_sups ) {
280 /* We are at the first recursive level */
288 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
289 sizeof(ObjectClass *));
295 soc1 = oc_find(*sups1);
298 return SLAP_SCHERR_CLASS_NOT_FOUND;
301 /* check object class usage
302 * abstract classes can only sup abstract classes
303 * structural classes can not sup auxiliary classes
304 * auxiliary classes can not sup structural classes
306 if( soc->soc_kind != soc1->soc_kind
307 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
310 return SLAP_SCHERR_CLASS_BAD_SUP;
313 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
315 return SLAP_SCHERR_CLASS_BAD_SUP;
318 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
321 soc->soc_sups[nsups] = soc1;
324 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
325 if ( code ) return code;
327 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
328 if ( code ) return code;
330 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
331 if ( code ) return code;
346 avl_free(oc_index, ldap_memfree);
347 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
348 o = LDAP_SLIST_FIRST(&oc_list);
349 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
351 if (o->soc_sups) ldap_memfree(o->soc_sups);
352 if (o->soc_required) ldap_memfree(o->soc_required);
353 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
354 ldap_objectclass_free((LDAPObjectClass *)o);
364 struct oindexrec *oir;
367 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
368 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
370 if ( soc->soc_oid ) {
371 oir = (struct oindexrec *)
372 ch_calloc( 1, sizeof(struct oindexrec) );
373 oir->oir_name.bv_val = soc->soc_oid;
374 oir->oir_name.bv_len = strlen( soc->soc_oid );
377 assert( oir->oir_name.bv_val );
378 assert( oir->oir_oc );
380 if ( avl_insert( &oc_index, (caddr_t) oir,
381 oc_index_cmp, avl_dup_error ) )
385 return SLAP_SCHERR_CLASS_DUP;
388 /* FIX: temporal consistency check */
389 assert( oc_bvfind(&oir->oir_name) != NULL );
392 if ( (names = soc->soc_names) ) {
394 oir = (struct oindexrec *)
395 ch_calloc( 1, sizeof(struct oindexrec) );
396 oir->oir_name.bv_val = *names;
397 oir->oir_name.bv_len = strlen( *names );
400 assert( oir->oir_name.bv_val );
401 assert( oir->oir_oc );
403 if ( avl_insert( &oc_index, (caddr_t) oir,
404 oc_index_cmp, avl_dup_error ) )
408 return SLAP_SCHERR_CLASS_DUP;
411 /* FIX: temporal consistency check */
412 assert( oc_bvfind(&oir->oir_name) != NULL );
432 if ( oc->oc_names != NULL ) {
435 for( i=0; oc->oc_names[i]; i++ ) {
436 if( !slap_valid_descr( oc->oc_names[i] ) ) {
437 return SLAP_SCHERR_BAD_DESCR;
442 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
443 /* Expand OID macros */
444 char *oid = oidm_find( oc->oc_oid );
447 return SLAP_SCHERR_OIDM;
449 if ( oid != oc->oc_oid ) {
450 ldap_memfree( oc->oc_oid );
455 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
456 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
458 if( oc->oc_names != NULL ) {
459 soc->soc_cname.bv_val = soc->soc_names[0];
461 soc->soc_cname.bv_val = soc->soc_oid;
463 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
465 if( soc->soc_sup_oids == NULL &&
466 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
468 /* structural object classes implicitly inherit from 'top' */
469 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
470 code = oc_add_sups( soc, top_oids, &op, err );
472 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
475 if ( code != 0 ) return code;
477 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
478 if ( code != 0 ) return code;
480 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
481 if ( code != 0 ) return code;
483 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
485 code = oc_insert(soc,err);
490 oc_schema_info( Entry *e )
492 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
497 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
498 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
500 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
505 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
506 (long) val.bv_len, val.bv_val, 0 );
508 nval.bv_val = oc->soc_oid;
509 nval.bv_len = strlen(oc->soc_oid);
511 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) )
515 ldap_memfree( val.bv_val );