1 /* oc.c - object class routines */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
19 int is_object_subclass(
25 if( sub == NULL || sup == NULL ) return 0;
29 LDAP_LOG ( OPERATION, ARGS,
30 "is_object_subclass(%s,%s) %d\n",
31 sup->soc_oid, sub->soc_oid, sup == sub );
33 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
34 sup->soc_oid, sub->soc_oid, sup == sub );
42 if( sub->soc_sups == NULL ) {
46 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
47 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
55 int is_entry_objectclass(
61 * set_flags should only be true if oc is one of operational
62 * object classes which we support objectClass flags for
63 * (e.g., referral, alias, ...). See <slap.h>.
68 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
70 assert(!( e == NULL || oc == NULL ));
72 if( e == NULL || oc == NULL ) {
76 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
77 /* flags are set, use them */
78 return e->e_ocflags & oc->soc_flags & SLAP_OC__MASK;
82 * find objectClass attribute
84 attr = attr_find(e->e_attrs, objectClass);
86 /* no objectClass attribute */
88 LDAP_LOG( OPERATION, ERR,
89 "is_entry_objectclass: dn(%s), oid (%s), no objectClass "
90 "attribute.\n", e->e_dn == NULL ? "" : e->e_dn,
91 oc->soc_oclass.oc_oid, 0 );
93 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
94 "no objectClass attribute\n",
95 e->e_dn == NULL ? "" : e->e_dn,
96 oc->soc_oclass.oc_oid, 0 );
102 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
103 ObjectClass *objectClass = oc_bvfind( bv );
105 if ( !set_flags && objectClass == oc ) {
109 if ( objectClass != NULL ) {
110 e->e_ocflags |= objectClass->soc_flags;
114 /* mark flags as set */
115 e->e_ocflags |= SLAP_OC__END;
117 return e->e_ocflags & oc->soc_flags & SLAP_OC__MASK;
122 struct berval oir_name;
126 static Avlnode *oc_index = NULL;
127 static ObjectClass *oc_list = NULL;
134 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
135 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
138 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
146 const struct berval *name = v_name;
147 const struct oindexrec *oir = v_oir;
148 int i = name->bv_len - oir->oir_name.bv_len;
151 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
155 oc_find( const char *ocname )
159 bv.bv_val = (char *)ocname;
160 bv.bv_len = strlen( ocname );
162 return( oc_bvfind( &bv ) );
166 oc_bvfind( struct berval *ocname )
168 struct oindexrec *oir;
170 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
173 return( oir->oir_oc );
188 AttributeType **satp;
194 sat = at_find(*attrs1);
197 return SLAP_SCHERR_ATTR_NOT_FOUND;
200 if( is_at_operational( sat )) (*op)++;
202 if ( at_find_in_list(sat, soc->soc_required) < 0) {
203 if ( at_append_to_list(sat, &soc->soc_required) ) {
205 return SLAP_SCHERR_OUTOFMEM;
210 /* Now delete duplicates from the allowed list */
211 for ( satp = soc->soc_required; *satp; satp++ ) {
212 i = at_find_in_list(*satp,soc->soc_allowed);
214 at_delete_from_list(i, &soc->soc_allowed);
234 sat = at_find(*attrs1);
237 return SLAP_SCHERR_ATTR_NOT_FOUND;
240 if( is_at_operational( sat )) (*op)++;
242 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
243 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
244 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
246 return SLAP_SCHERR_OUTOFMEM;
269 if ( !soc->soc_sups ) {
270 /* We are at the first recursive level */
278 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
279 sizeof(ObjectClass *));
285 soc1 = oc_find(*sups1);
288 return SLAP_SCHERR_CLASS_NOT_FOUND;
291 /* check object class usage
292 * abstract classes can only sup abstract classes
293 * structural classes can not sup auxiliary classes
294 * auxiliary classes can not sup structural classes
296 if( soc->soc_kind != soc1->soc_kind
297 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
300 return SLAP_SCHERR_CLASS_BAD_SUP;
303 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
305 return SLAP_SCHERR_CLASS_BAD_SUP;
308 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
311 soc->soc_sups[nsups] = soc1;
314 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
315 if ( code ) return code;
317 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
318 if ( code ) return code;
320 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
321 if ( code ) return code;
336 avl_free(oc_index, ldap_memfree);
337 for (o=oc_list; o; o=n)
340 if (o->soc_sups) ldap_memfree(o->soc_sups);
341 if (o->soc_required) ldap_memfree(o->soc_required);
342 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
343 ldap_objectclass_free((LDAPObjectClass *)o);
354 struct oindexrec *oir;
358 while ( *ocp != NULL ) {
359 ocp = &(*ocp)->soc_next;
363 if ( soc->soc_oid ) {
364 oir = (struct oindexrec *)
365 ch_calloc( 1, sizeof(struct oindexrec) );
366 oir->oir_name.bv_val = soc->soc_oid;
367 oir->oir_name.bv_len = strlen( soc->soc_oid );
370 assert( oir->oir_name.bv_val );
371 assert( oir->oir_oc );
373 if ( avl_insert( &oc_index, (caddr_t) oir,
374 oc_index_cmp, avl_dup_error ) )
378 return SLAP_SCHERR_CLASS_DUP;
381 /* FIX: temporal consistency check */
382 assert( oc_bvfind(&oir->oir_name) != NULL );
385 if ( (names = soc->soc_names) ) {
387 oir = (struct oindexrec *)
388 ch_calloc( 1, sizeof(struct oindexrec) );
389 oir->oir_name.bv_val = *names;
390 oir->oir_name.bv_len = strlen( *names );
393 assert( oir->oir_name.bv_val );
394 assert( oir->oir_oc );
396 if ( avl_insert( &oc_index, (caddr_t) oir,
397 oc_index_cmp, avl_dup_error ) )
401 return SLAP_SCHERR_CLASS_DUP;
404 /* FIX: temporal consistency check */
405 assert( oc_bvfind(&oir->oir_name) != NULL );
425 if ( oc->oc_names != NULL ) {
428 for( i=0; oc->oc_names[i]; i++ ) {
429 if( !slap_valid_descr( oc->oc_names[i] ) ) {
430 return SLAP_SCHERR_BAD_DESCR;
435 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
436 /* Expand OID macros */
437 char *oid = oidm_find( oc->oc_oid );
440 return SLAP_SCHERR_OIDM;
442 if ( oid != oc->oc_oid ) {
443 ldap_memfree( oc->oc_oid );
448 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
449 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
451 if( oc->oc_names != NULL ) {
452 soc->soc_cname.bv_val = soc->soc_names[0];
454 soc->soc_cname.bv_val = soc->soc_oid;
456 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
458 if( soc->soc_sup_oids == NULL &&
459 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
461 /* structural object classes implicitly inherit from 'top' */
462 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
463 code = oc_add_sups( soc, top_oids, &op, err );
465 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
468 if ( code != 0 ) return code;
470 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
471 if ( code != 0 ) return code;
473 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
474 if ( code != 0 ) return code;
476 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
478 code = oc_insert(soc,err);
483 oc_schema_info( Entry *e )
485 struct berval vals[2];
488 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
490 vals[1].bv_val = NULL;
492 for ( oc = oc_list; oc; oc = oc->soc_next ) {
493 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
495 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
500 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
501 (long) vals[0].bv_len, vals[0].bv_val, 0 );
503 if( attr_merge( e, ad_objectClasses, vals ) )
505 ldap_memfree( vals[0].bv_val );