1 /* schema.c - routines to enforce schema definitions */
12 static struct objclass *oc_find(char *ocname);
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 )
29 /* find the object class attribute - could error out here */
30 if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
31 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
36 /* check that the entry has required attrs for each oc */
37 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
38 char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
41 Debug( LDAP_DEBUG_ANY,
42 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
43 e->e_dn, aoc->a_vals[i]->bv_val, s );
52 /* check that each attr in the entry is allowed by some oc */
53 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
54 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
55 Debug( LDAP_DEBUG_ANY,
56 "Entry (%s), attr \"%s\" not allowed\n",
57 e->e_dn, a->a_type, 0 );
66 oc_check_required( Entry *e, char *ocname )
72 /* find global oc defn. it we don't know about it assume it's ok */
73 if ( (oc = oc_find( ocname )) == NULL ) {
77 /* check for empty oc_required */
78 if(oc->oc_required == NULL) {
82 /* for each required attribute */
83 for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
84 /* see if it's in the entry */
85 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
86 if ( strcasecmp( a->a_type, oc->oc_required[i] )
92 /* not there => schema violation */
94 return oc->oc_required[i];
102 * check to see if attribute is 'operational' or not.
103 * this function should be externalized...
106 oc_check_operational( char *type )
108 return ( strcasecmp( type, "modifiersname" ) == 0 ||
109 strcasecmp( type, "modifytimestamp" ) == 0 ||
110 strcasecmp( type, "creatorsname" ) == 0 ||
111 strcasecmp( type, "createtimestamp" ) == 0 )
116 oc_check_allowed( char *type, struct berval **ocl )
121 /* always allow objectclass attribute */
122 if ( strcasecmp( type, "objectclass" ) == 0 ) {
126 if ( oc_check_operational( type ) ) {
130 /* check that the type appears as req or opt in at least one oc */
131 for ( i = 0; ocl[i] != NULL; i++ ) {
132 /* if we know about the oc */
133 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
134 /* does it require the type? */
135 for ( j = 0; oc->oc_required != NULL &&
136 oc->oc_required[j] != NULL; j++ ) {
137 if ( strcasecmp( oc->oc_required[j], type )
142 /* does it allow the type? */
143 for ( j = 0; oc->oc_allowed != NULL &&
144 oc->oc_allowed[j] != NULL; j++ ) {
145 if ( strcasecmp( oc->oc_allowed[j], type )
146 == 0 || strcmp( oc->oc_allowed[j], "*" )
152 /* maybe the next oc allows it */
154 /* we don't know about the oc. assume it allows it */
160 /* not allowed by any oc */
164 static struct objclass *
165 oc_find( char *ocname )
169 for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
170 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
181 oc_print( struct objclass *oc )
185 printf( "objectclass %s\n", oc->oc_name );
186 if ( oc->oc_required != NULL ) {
187 printf( "\trequires %s", oc->oc_required[0] );
188 for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
189 printf( ",%s", oc->oc_required[i] );
193 if ( oc->oc_allowed != NULL ) {
194 printf( "\tallows %s", oc->oc_allowed[0] );
195 for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
196 printf( ",%s", oc->oc_allowed[i] );