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;
147 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
155 const struct berval *name = v_name;
156 const struct oindexrec *oir = v_oir;
157 int i = name->bv_len - oir->oir_name.bv_len;
159 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
163 oc_find( const char *ocname )
167 bv.bv_val = (char *)ocname;
168 bv.bv_len = strlen( ocname );
170 return( oc_bvfind( &bv ) );
174 oc_bvfind( struct berval *ocname )
176 struct oindexrec *oir;
178 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
181 return( oir->oir_oc );
196 AttributeType **satp;
202 sat = at_find(*attrs1);
205 return SLAP_SCHERR_ATTR_NOT_FOUND;
208 if( is_at_operational( sat )) (*op)++;
210 if ( at_find_in_list(sat, soc->soc_required) < 0) {
211 if ( at_append_to_list(sat, &soc->soc_required) ) {
213 return SLAP_SCHERR_OUTOFMEM;
218 /* Now delete duplicates from the allowed list */
219 for ( satp = soc->soc_required; *satp; satp++ ) {
220 i = at_find_in_list(*satp,soc->soc_allowed);
222 at_delete_from_list(i, &soc->soc_allowed);
242 sat = at_find(*attrs1);
245 return SLAP_SCHERR_ATTR_NOT_FOUND;
248 if( is_at_operational( sat )) (*op)++;
250 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
251 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
252 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
254 return SLAP_SCHERR_OUTOFMEM;
277 if ( !soc->soc_sups ) {
278 /* We are at the first recursive level */
286 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
287 sizeof(ObjectClass *));
293 soc1 = oc_find(*sups1);
296 return SLAP_SCHERR_CLASS_NOT_FOUND;
299 /* check object class usage
300 * abstract classes can only sup abstract classes
301 * structural classes can not sup auxiliary classes
302 * auxiliary classes can not sup structural classes
304 if( soc->soc_kind != soc1->soc_kind
305 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
308 return SLAP_SCHERR_CLASS_BAD_SUP;
311 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
313 return SLAP_SCHERR_CLASS_BAD_SUP;
316 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
319 soc->soc_sups[nsups] = soc1;
322 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
323 if ( code ) return code;
325 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
326 if ( code ) return code;
328 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
329 if ( code ) return code;
344 avl_free(oc_index, ldap_memfree);
345 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
346 o = LDAP_SLIST_FIRST(&oc_list);
347 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
349 if (o->soc_sups) ldap_memfree(o->soc_sups);
350 if (o->soc_required) ldap_memfree(o->soc_required);
351 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
352 ldap_objectclass_free((LDAPObjectClass *)o);
361 struct oindexrec *oir;
364 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
365 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
367 if ( soc->soc_oid ) {
368 oir = (struct oindexrec *)
369 ch_calloc( 1, sizeof(struct oindexrec) );
370 oir->oir_name.bv_val = soc->soc_oid;
371 oir->oir_name.bv_len = strlen( soc->soc_oid );
374 assert( oir->oir_name.bv_val );
375 assert( oir->oir_oc );
377 if ( avl_insert( &oc_index, (caddr_t) oir,
378 oc_index_cmp, avl_dup_error ) )
382 return SLAP_SCHERR_CLASS_DUP;
385 /* FIX: temporal consistency check */
386 assert( oc_bvfind(&oir->oir_name) != NULL );
389 if ( (names = soc->soc_names) ) {
391 oir = (struct oindexrec *)
392 ch_calloc( 1, sizeof(struct oindexrec) );
393 oir->oir_name.bv_val = *names;
394 oir->oir_name.bv_len = strlen( *names );
397 assert( oir->oir_name.bv_val );
398 assert( oir->oir_oc );
400 if ( avl_insert( &oc_index, (caddr_t) oir,
401 oc_index_cmp, avl_dup_error ) )
405 return SLAP_SCHERR_CLASS_DUP;
408 /* FIX: temporal consistency check */
409 assert( oc_bvfind(&oir->oir_name) != NULL );
428 if ( oc->oc_names != NULL ) {
431 for( i=0; oc->oc_names[i]; i++ ) {
432 if( !slap_valid_descr( oc->oc_names[i] ) ) {
433 return SLAP_SCHERR_BAD_DESCR;
438 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
439 /* Expand OID macros */
440 char *oid = oidm_find( oc->oc_oid );
443 return SLAP_SCHERR_OIDM;
445 if ( oid != oc->oc_oid ) {
446 ldap_memfree( oc->oc_oid );
451 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
452 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
454 if( oc->oc_names != NULL ) {
455 soc->soc_cname.bv_val = soc->soc_names[0];
457 soc->soc_cname.bv_val = soc->soc_oid;
459 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
461 if( soc->soc_sup_oids == NULL &&
462 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
464 /* structural object classes implicitly inherit from 'top' */
465 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
466 code = oc_add_sups( soc, top_oids, &op, err );
468 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
471 if ( code != 0 ) return code;
472 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
474 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
475 if ( code != 0 ) return code;
477 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
478 if ( code != 0 ) return code;
480 if( user && op ) return SLAP_SCHERR_CLASS_BAD_USAGE;
482 code = oc_insert(soc,err);
487 oc_schema_info( Entry *e )
489 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
494 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
495 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
497 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
501 nval = oc->soc_cname;
504 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
505 (long) val.bv_len, val.bv_val, nval.bv_val );
508 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
511 ldap_memfree( val.bv_val );