1 /* schema.c - routines to enforce schema definitions */
8 #include <sys/socket.h>
11 extern Attribute *attr_find();
12 extern char **str2charray();
13 extern void charray_merge();
15 extern struct objclass *global_oc;
16 extern int global_schemacheck;
18 static struct objclass *oc_find();
19 static int oc_check_required();
20 static int oc_check_allowed();
23 * oc_check - check that entry e conforms to the schema required by
24 * its object class(es). returns 0 if so, non-zero otherwise.
28 oc_schema_check( Entry *e )
35 /* find the object class attribute - could error out here */
36 if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
37 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
42 /* check that the entry has required attrs for each oc */
43 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
44 if ( oc_check_required( e, aoc->a_vals[i]->bv_val ) != 0 ) {
45 Debug( LDAP_DEBUG_ANY,
46 "Entry (%s), required attr (%s) missing\n",
47 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
56 /* check that each attr in the entry is allowed by some oc */
57 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
58 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
59 Debug( LDAP_DEBUG_ANY,
60 "Entry (%s), attr (%s) not allowed\n",
61 e->e_dn, a->a_type, 0 );
70 oc_check_required( Entry *e, char *ocname )
76 /* find global oc defn. it we don't know about it assume it's ok */
77 if ( (oc = oc_find( ocname )) == NULL ) {
81 /* check for empty oc_required */
82 if(oc->oc_required == NULL) {
86 /* for each required attribute */
87 for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
88 /* see if it's in the entry */
89 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
90 if ( strcasecmp( a->a_type, oc->oc_required[i] )
96 /* not there => schema violation */
106 oc_check_allowed( char *type, struct berval **ocl )
111 /* always allow objectclass attribute */
112 if ( strcasecmp( type, "objectclass" ) == 0 ) {
116 /* check that the type appears as req or opt in at least one oc */
117 for ( i = 0; ocl[i] != NULL; i++ ) {
118 /* if we know about the oc */
119 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
120 /* does it require the type? */
121 for ( j = 0; oc->oc_required != NULL &&
122 oc->oc_required[j] != NULL; j++ ) {
123 if ( strcasecmp( oc->oc_required[j], type )
128 /* does it allow the type? */
129 for ( j = 0; oc->oc_allowed != NULL &&
130 oc->oc_allowed[j] != NULL; j++ ) {
131 if ( strcasecmp( oc->oc_allowed[j], type )
132 == 0 || strcmp( oc->oc_allowed[j], "*" )
138 /* maybe the next oc allows it */
140 /* we don't know about the oc. assume it allows it */
146 /* not allowed by any oc */
150 static struct objclass *
151 oc_find( char *ocname )
155 for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
156 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
167 oc_print( struct objclass *oc )
171 printf( "objectclass %s\n", oc->oc_name );
172 if ( oc->oc_required != NULL ) {
173 printf( "\trequires %s", oc->oc_required[0] );
174 for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
175 printf( ",%s", oc->oc_required[i] );
179 if ( oc->oc_allowed != NULL ) {
180 printf( "\tallows %s", oc->oc_allowed[0] );
181 for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
182 printf( ",%s", oc->oc_allowed[i] );