1 /* schema.c - routines to enforce schema definitions */
10 #include "ldapconfig.h"
13 static char * oc_check_required(Entry *e, char *ocname);
14 static int oc_check_allowed(char *type, struct berval **ocl);
17 * oc_check - check that entry e conforms to the schema required by
18 * its object class(es). returns 0 if so, non-zero otherwise.
22 oc_schema_check( Entry *e )
28 /* find the object class attribute - could error out here */
29 if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
30 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
35 /* check that the entry has required attrs for each oc */
36 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
37 char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
40 Debug( LDAP_DEBUG_ANY,
41 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
42 e->e_dn, aoc->a_vals[i]->bv_val, s );
51 /* check that each attr in the entry is allowed by some oc */
52 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
53 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
54 Debug( LDAP_DEBUG_ANY,
55 "Entry (%s), attr \"%s\" not allowed\n",
56 e->e_dn, a->a_type, 0 );
65 oc_check_required( Entry *e, char *ocname )
73 Debug( LDAP_DEBUG_TRACE,
74 "oc_check_required entry (%s), objectclass \"%s\"\n",
77 /* find global oc defn. it we don't know about it assume it's ok */
78 if ( (oc = oc_find( ocname )) == NULL ) {
82 /* check for empty oc_required */
83 if(oc->soc_required == NULL) {
87 /* for each required attribute */
88 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
89 at = oc->soc_required[i];
90 /* see if it's in the entry */
91 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
93 strcmp( a->a_type, at->sat_oid ) == 0 ) {
98 /* Empty name list => not found */
103 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
112 /* not there => schema violation */
114 if ( at->sat_names && at->sat_names[0] ) {
115 return at->sat_names[0];
126 * check to see if attribute is 'operational' or not.
127 * this function should be externalized...
130 oc_check_operational( char *type )
132 return ( strcasecmp( type, "modifiersname" ) == 0 ||
133 strcasecmp( type, "modifytimestamp" ) == 0 ||
134 strcasecmp( type, "creatorsname" ) == 0 ||
135 strcasecmp( type, "createtimestamp" ) == 0 )
140 oc_check_allowed( char *type, struct berval **ocl )
147 Debug( LDAP_DEBUG_TRACE,
148 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
150 /* always allow objectclass attribute */
151 if ( strcasecmp( type, "objectclass" ) == 0 ) {
155 if ( oc_check_operational( type ) ) {
159 /* check that the type appears as req or opt in at least one oc */
160 for ( i = 0; ocl[i] != NULL; i++ ) {
161 /* if we know about the oc */
162 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
163 /* does it require the type? */
164 for ( j = 0; oc->soc_required != NULL &&
165 oc->soc_required[j] != NULL; j++ ) {
166 at = oc->soc_required[j];
168 strcmp(at->sat_oid, type ) == 0 ) {
175 if ( strcasecmp( *pp, type ) == 0 ) {
181 /* does it allow the type? */
182 for ( j = 0; oc->soc_allowed != NULL &&
183 oc->soc_allowed[j] != NULL; j++ ) {
184 at = oc->soc_allowed[j];
186 strcmp(at->sat_oid, type ) == 0 ) {
193 if ( strcasecmp( *pp, type ) == 0 ||
194 strcmp( *pp, "*" ) == 0 ) {
200 /* maybe the next oc allows it */
202 /* we don't know about the oc. assume it allows it */
208 /* not allowed by any oc */
217 static Avlnode *oc_index = NULL;
218 static ObjectClass *oc_list = NULL;
222 struct oindexrec *oir1,
223 struct oindexrec *oir2
226 return (strcasecmp( oir1->oir_name, oir2->oir_name ));
232 struct oindexrec *oir
235 return (strcasecmp( type, oir->oir_name ));
239 oc_find( char *ocname )
241 struct oindexrec *oir = NULL;
243 if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
244 (AVL_CMP) oc_index_name_cmp )) != NULL ) {
245 return( oir->oir_oc );
259 AttributeType **satp;
265 sat = at_find(*attrs1);
268 return SLAP_SCHERR_ATTR_NOT_FOUND;
270 if ( at_find_in_list(sat, soc->soc_required) < 0) {
271 if ( at_append_to_list(sat, &soc->soc_required) ) {
273 return SLAP_SCHERR_OUTOFMEM;
278 /* Now delete duplicates from the allowed list */
279 for ( satp = soc->soc_required; *satp; satp++ ) {
280 i = at_find_in_list(*satp,soc->soc_allowed);
282 at_delete_from_list(i, &soc->soc_allowed);
302 sat = at_find(*attrs1);
305 return SLAP_SCHERR_ATTR_NOT_FOUND;
307 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
308 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
309 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
311 return SLAP_SCHERR_OUTOFMEM;
334 if ( !soc->soc_sups ) {
335 /* We are at the first recursive level */
344 soc->soc_sups = (ObjectClass **)ch_calloc(1,
345 nsups*sizeof(ObjectClass *));
350 soc1 = oc_find(*sups1);
353 return SLAP_SCHERR_CLASS_NOT_FOUND;
357 soc->soc_sups[nsups] = soc1;
359 code = oc_add_sups(soc,soc1->soc_sup_oids, err);
363 if ( code = oc_create_required(soc,
364 soc1->soc_at_oids_must,err) )
366 if ( code = oc_create_allowed(soc,
367 soc1->soc_at_oids_may,err) )
383 struct oindexrec *oir;
387 while ( *ocp != NULL ) {
388 ocp = &(*ocp)->soc_next;
392 if ( soc->soc_oid ) {
393 oir = (struct oindexrec *)
394 ch_calloc( 1, sizeof(struct oindexrec) );
395 oir->oir_name = soc->soc_oid;
397 if ( avl_insert( &oc_index, (caddr_t) oir,
398 (AVL_CMP) oc_index_cmp,
399 (AVL_DUP) avl_dup_error ) ) {
402 return SLAP_SCHERR_DUP_CLASS;
404 /* FIX: temporal consistency check */
405 oc_find(oir->oir_name);
407 if ( (names = soc->soc_names) ) {
409 oir = (struct oindexrec *)
410 ch_calloc( 1, sizeof(struct oindexrec) );
411 oir->oir_name = ch_strdup(*names);
413 if ( avl_insert( &oc_index, (caddr_t) oir,
414 (AVL_CMP) oc_index_cmp,
415 (AVL_DUP) avl_dup_error ) ) {
418 return SLAP_SCHERR_DUP_CLASS;
420 /* FIX: temporal consistency check */
421 oc_find(oir->oir_name);
430 LDAP_OBJECT_CLASS *oc,
437 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
438 memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
439 if ( code = oc_add_sups(soc,soc->soc_sup_oids,err) )
441 if ( code = oc_create_required(soc,soc->soc_at_oids_must,err) )
443 if ( code = oc_create_allowed(soc,soc->soc_at_oids_may,err) )
445 code = oc_insert(soc,err);
449 #if defined( SLAPD_SCHEMA_DN )
452 oc_schema_info( Entry *e )
455 struct berval *vals[2];
461 for ( oc = oc_list; oc; oc = oc->soc_next ) {
462 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
464 val.bv_len = strlen( val.bv_val );
465 Debug( LDAP_DEBUG_TRACE, "Merging oc [%d] %s\n",
466 val.bv_len, val.bv_val, 0 );
467 attr_merge( e, "objectclasses", vals );
468 ldap_memfree( val.bv_val );
477 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
481 struct berval *vals[2];
486 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
489 e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
490 e->e_ndn = dn_normalize_case( ch_strdup( SLAPD_SCHEMA_DN ));
493 val.bv_val = ch_strdup( "top" );
494 val.bv_len = strlen( val.bv_val );
495 attr_merge( e, "objectclass", vals );
496 ldap_memfree( val.bv_val );
498 val.bv_val = ch_strdup( "subschema" );
499 val.bv_len = strlen( val.bv_val );
500 attr_merge( e, "objectclass", vals );
501 ldap_memfree( val.bv_val );
503 if ( at_schema_info( e ) ) {
504 /* Out of memory, do something about it */
508 if ( oc_schema_info( e ) ) {
509 /* Out of memory, do something about it */
514 send_search_entry( &backends[0], conn, op, e, attrs, attrsonly );
515 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL, 1 );
524 oc_print( ObjectClass *oc )
528 if ( oc->soc_names && oc->soc_names[0] ) {
529 printf( "objectclass %s\n", oc->soc_names[0] );
531 printf( "objectclass %s\n", oc->soc_oid );
533 if ( oc->soc_required != NULL ) {
534 printf( "\trequires %s", oc->soc_required[0] );
535 for ( i = 1; oc->soc_required[i] != NULL; i++ ) {
536 printf( ",%s", oc->soc_required[i] );
540 if ( oc->soc_allowed != NULL ) {
541 printf( "\tallows %s", oc->soc_allowed[0] );
542 for ( i = 1; oc->soc_allowed[i] != NULL; i++ ) {
543 printf( ",%s", oc->soc_allowed[i] );