]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
merged with autoconf branch
[openldap] / servers / slapd / schema.c
1 /* schema.c - routines to enforce schema definitions */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11
12 extern Attribute        *attr_find();
13 extern char             **str2charray();
14 extern void             charray_merge();
15
16 extern struct objclass  *global_oc;
17 extern int              global_schemacheck;
18
19 static struct objclass  *oc_find();
20 static int              oc_check_required();
21 static int              oc_check_allowed();
22
23 /*
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.
26  */
27
28 int
29 oc_schema_check( Entry *e )
30 {
31         Attribute       *a, *aoc;
32         struct objclass *oc;
33         int             i;
34         int             ret = 0;
35
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",
39                     e->e_dn, 0, 0 );
40                 return( 0 );
41         }
42
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 );
49                         ret = 1;
50                 }
51         }
52
53         if ( ret != 0 ) {
54             return( ret );
55         }
56
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 );
63                         ret = 1;
64                 }
65         }
66
67         return( ret );
68 }
69
70 static int
71 oc_check_required( Entry *e, char *ocname )
72 {
73         struct objclass *oc;
74         int             i;
75         Attribute       *a;
76
77         /* find global oc defn. it we don't know about it assume it's ok */
78         if ( (oc = oc_find( ocname )) == NULL ) {
79                 return( 0 );
80         }
81
82         /* check for empty oc_required */
83         if(oc->oc_required == NULL) {
84                 return( 0 );
85         }
86
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] )
92                             == 0 ) {
93                                 break;
94                         }
95                 }
96
97                 /* not there => schema violation */
98                 if ( a == NULL ) {
99                         return( 1 );
100                 }
101         }
102
103         return( 0 );
104 }
105
106 static int
107 oc_check_allowed( char *type, struct berval **ocl )
108 {
109         struct objclass *oc;
110         int             i, j;
111
112         /* always allow objectclass attribute */
113         if ( strcasecmp( type, "objectclass" ) == 0 ) {
114                 return( 0 );
115         }
116
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 )
125                                     == 0 ) {
126                                         return( 0 );
127                                 }
128                         }
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], "*" )
134                                     == 0 )
135                                 {
136                                         return( 0 );
137                                 }
138                         }
139                         /* maybe the next oc allows it */
140
141                 /* we don't know about the oc. assume it allows it */
142                 } else {
143                         return( 0 );
144                 }
145         }
146
147         /* not allowed by any oc */
148         return( 1 );
149 }
150
151 static struct objclass *
152 oc_find( char *ocname )
153 {
154         struct objclass *oc;
155
156         for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
157                 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
158                         return( oc );
159                 }
160         }
161
162         return( NULL );
163 }
164
165 #ifdef LDAP_DEBUG
166
167 static
168 oc_print( struct objclass *oc )
169 {
170         int     i;
171
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] );
177                 }
178                 printf( "\n" );
179         }
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] );
184                 }
185                 printf( "\n" );
186         }
187 }
188
189 #endif