1 /* schema.c - routines to enforce schema definitions */
12 extern Attribute *attr_find();
13 extern char **str2charray();
14 extern void charray_merge();
16 extern struct objclass *global_oc;
17 extern int global_schemacheck;
19 static struct objclass *oc_find();
20 static int oc_check_required();
21 static int oc_check_allowed();
24 * oc_check - check that entry e conforms to the schema required by
25 * its object class(es). returns 0 if so, non-zero otherwise.
29 oc_schema_check( Entry *e )
36 /* find the object class attribute - could error out here */
37 if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
38 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
43 /* check that the entry has required attrs for each oc */
44 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
45 if ( oc_check_required( e, aoc->a_vals[i]->bv_val ) != 0 ) {
46 Debug( LDAP_DEBUG_ANY,
47 "Entry (%s), required attr (%s) missing\n",
48 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
57 /* check that each attr in the entry is allowed by some oc */
58 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
59 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
60 Debug( LDAP_DEBUG_ANY,
61 "Entry (%s), attr (%s) not allowed\n",
62 e->e_dn, a->a_type, 0 );
71 oc_check_required( Entry *e, char *ocname )
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->oc_required == NULL) {
87 /* for each required attribute */
88 for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
89 /* see if it's in the entry */
90 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
91 if ( strcasecmp( a->a_type, oc->oc_required[i] )
97 /* not there => schema violation */
107 oc_check_allowed( char *type, struct berval **ocl )
112 /* always allow objectclass attribute */
113 if ( strcasecmp( type, "objectclass" ) == 0 ) {
117 /* check that the type appears as req or opt in at least one oc */
118 for ( i = 0; ocl[i] != NULL; i++ ) {
119 /* if we know about the oc */
120 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
121 /* does it require the type? */
122 for ( j = 0; oc->oc_required != NULL &&
123 oc->oc_required[j] != NULL; j++ ) {
124 if ( strcasecmp( oc->oc_required[j], type )
129 /* does it allow the type? */
130 for ( j = 0; oc->oc_allowed != NULL &&
131 oc->oc_allowed[j] != NULL; j++ ) {
132 if ( strcasecmp( oc->oc_allowed[j], type )
133 == 0 || strcmp( oc->oc_allowed[j], "*" )
139 /* maybe the next oc allows it */
141 /* we don't know about the oc. assume it allows it */
147 /* not allowed by any oc */
151 static struct objclass *
152 oc_find( char *ocname )
156 for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
157 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
168 oc_print( struct objclass *oc )
172 printf( "objectclass %s\n", oc->oc_name );
173 if ( oc->oc_required != NULL ) {
174 printf( "\trequires %s", oc->oc_required[0] );
175 for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
176 printf( ",%s", oc->oc_required[i] );
180 if ( oc->oc_allowed != NULL ) {
181 printf( "\tallows %s", oc->oc_allowed[0] );
182 for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
183 printf( ",%s", oc->oc_allowed[i] );