1 /* oc.c - object class routines */
4 * Copyright 1998-2002 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(
62 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
63 assert(!( e == NULL || oc == NULL ));
65 if( e == NULL || oc == NULL ) {
69 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
70 return (e->e_ocflags & oc->soc_flags) ? 1 : 0;
74 * find objectClass attribute
76 attr = attr_find(e->e_attrs, objectClass);
79 /* no objectClass attribute */
81 LDAP_LOG( OPERATION, ERR,
82 "is_entry_objectclass: dn(%s), oid (%s), no objectClass "
83 "attribute.\n", e->e_dn == NULL ? "" : e->e_dn,
84 oc->soc_oclass.oc_oid, 0 );
86 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
87 "no objectClass attribute\n",
88 e->e_dn == NULL ? "" : e->e_dn,
89 oc->soc_oclass.oc_oid, 0 );
95 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
96 ObjectClass *objectClass = oc_bvfind( bv );
98 if ( objectClass == oc && !set_flags ) {
102 if ( objectClass != NULL ) {
103 e->e_ocflags |= objectClass->soc_flags;
106 e->e_ocflags |= SLAP_OC__END; /* We've finished this */
108 return (e->e_ocflags & oc->soc_flags);
113 struct berval oir_name;
117 static Avlnode *oc_index = NULL;
118 static ObjectClass *oc_list = NULL;
122 struct oindexrec *oir1,
123 struct oindexrec *oir2 )
125 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
128 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
134 struct oindexrec *oir )
136 int i = name->bv_len - oir->oir_name.bv_len;
139 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
143 oc_find( const char *ocname )
147 bv.bv_val = (char *)ocname;
148 bv.bv_len = strlen( ocname );
150 return( oc_bvfind( &bv ) );
154 oc_bvfind( struct berval *ocname )
156 struct oindexrec *oir;
158 oir = (struct oindexrec *) avl_find( oc_index, ocname,
159 (AVL_CMP) oc_index_name_cmp );
162 return( oir->oir_oc );
177 AttributeType **satp;
183 sat = at_find(*attrs1);
186 return SLAP_SCHERR_ATTR_NOT_FOUND;
189 if( is_at_operational( sat )) (*op)++;
191 if ( at_find_in_list(sat, soc->soc_required) < 0) {
192 if ( at_append_to_list(sat, &soc->soc_required) ) {
194 return SLAP_SCHERR_OUTOFMEM;
199 /* Now delete duplicates from the allowed list */
200 for ( satp = soc->soc_required; *satp; satp++ ) {
201 i = at_find_in_list(*satp,soc->soc_allowed);
203 at_delete_from_list(i, &soc->soc_allowed);
223 sat = at_find(*attrs1);
226 return SLAP_SCHERR_ATTR_NOT_FOUND;
229 if( is_at_operational( sat )) (*op)++;
231 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
232 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
233 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
235 return SLAP_SCHERR_OUTOFMEM;
258 if ( !soc->soc_sups ) {
259 /* We are at the first recursive level */
267 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
268 sizeof(ObjectClass *));
274 soc1 = oc_find(*sups1);
277 return SLAP_SCHERR_CLASS_NOT_FOUND;
280 /* check object class usage
281 * abstract classes can only sup abstract classes
282 * structural classes can not sup auxiliary classes
283 * auxiliary classes can not sup structural classes
285 if( soc->soc_kind != soc1->soc_kind
286 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
289 return SLAP_SCHERR_CLASS_BAD_USAGE;
292 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
295 soc->soc_sups[nsups] = soc1;
298 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
299 if ( code ) return code;
301 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
302 if ( code ) return code;
304 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
305 if ( code ) return code;
320 avl_free(oc_index, ldap_memfree);
321 for (o=oc_list; o; o=n)
324 if (o->soc_sups) ldap_memfree(o->soc_sups);
325 if (o->soc_required) ldap_memfree(o->soc_required);
326 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
327 ldap_objectclass_free((LDAPObjectClass *)o);
338 struct oindexrec *oir;
342 while ( *ocp != NULL ) {
343 ocp = &(*ocp)->soc_next;
347 if ( soc->soc_oid ) {
348 oir = (struct oindexrec *)
349 ch_calloc( 1, sizeof(struct oindexrec) );
350 oir->oir_name.bv_val = soc->soc_oid;
351 oir->oir_name.bv_len = strlen( soc->soc_oid );
354 assert( oir->oir_name.bv_val );
355 assert( oir->oir_oc );
357 if ( avl_insert( &oc_index, (caddr_t) oir,
358 (AVL_CMP) oc_index_cmp,
359 (AVL_DUP) avl_dup_error ) )
363 return SLAP_SCHERR_CLASS_DUP;
366 /* FIX: temporal consistency check */
367 assert( oc_bvfind(&oir->oir_name) != NULL );
370 if ( (names = soc->soc_names) ) {
372 oir = (struct oindexrec *)
373 ch_calloc( 1, sizeof(struct oindexrec) );
374 oir->oir_name.bv_val = *names;
375 oir->oir_name.bv_len = strlen( *names );
378 assert( oir->oir_name.bv_val );
379 assert( oir->oir_oc );
381 if ( avl_insert( &oc_index, (caddr_t) oir,
382 (AVL_CMP) oc_index_cmp,
383 (AVL_DUP) avl_dup_error ) )
387 return SLAP_SCHERR_CLASS_DUP;
390 /* FIX: temporal consistency check */
391 assert( oc_bvfind(&oir->oir_name) != NULL );
411 if ( oc->oc_names != NULL ) {
414 for( i=0; oc->oc_names[i]; i++ ) {
415 if( !slap_valid_descr( oc->oc_names[i] ) ) {
416 return SLAP_SCHERR_BAD_DESCR;
421 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
422 /* Expand OID macros */
423 char *oid = oidm_find( oc->oc_oid );
426 return SLAP_SCHERR_OIDM;
428 if ( oid != oc->oc_oid ) {
429 ldap_memfree( oc->oc_oid );
434 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
435 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
437 if( soc->soc_sup_oids == NULL &&
438 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
440 /* structural object classes implicitly inherit from 'top' */
441 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
442 code = oc_add_sups( soc, top_oids, &op, err );
444 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
447 if ( code != 0 ) return code;
449 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
450 if ( code != 0 ) return code;
452 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
453 if ( code != 0 ) return code;
455 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
457 code = oc_insert(soc,err);
464 oc_print( ObjectClass *oc )
469 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
470 if ( oc->soc_required != NULL ) {
472 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
474 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
477 if ( oc->soc_allowed != NULL ) {
479 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
481 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
489 #if defined( SLAPD_SCHEMA_DN )
492 oc_schema_info( Entry *e )
494 struct berval vals[2];
497 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
499 vals[1].bv_val = NULL;
501 for ( oc = oc_list; oc; oc = oc->soc_next ) {
502 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
506 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
509 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
510 (long) vals[0].bv_len, vals[0].bv_val, 0 );
512 attr_merge( e, ad_objectClasses, vals );
513 ldap_memfree( vals[0].bv_val );