]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
SLAPD_SCHEMA_NOT_COMPAT:
[openldap] / servers / slapd / schema_check.c
1 /* schema_check.c - routines to enforce schema definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_pvt.h"
18
19 #ifdef SLAPD_SCHEMA_NOT_COMPAT
20 static int oc_check_allowed(
21         AttributeType *type,
22         struct berval **oclist );
23 #else
24 static int              oc_check_allowed(char *type, struct berval **oclist);
25 #endif
26 static char *   oc_check_required(Entry *e, struct berval *ocname);
27
28 /*
29  * entry_schema_check - check that entry e conforms to the schema required
30  * by its object class(es).
31  *
32  * returns 0 if so, non-zero otherwise.
33  */
34
35 int
36 entry_schema_check( 
37         Entry *e, Attribute *oldattrs, const char** text )
38 {
39         Attribute       *a, *aoc;
40         ObjectClass *oc;
41         int             i;
42         int             ret;
43 #ifdef SLAPD_SCHEMA_NOT_COMPAT
44         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
45 #else
46         static const char *ad_objectClass = "objectclass";
47 #endif
48
49         if( !global_schemacheck ) return LDAP_SUCCESS;
50
51         /* find the object class attribute - could error out here */
52         if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
53                 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
54                     e->e_dn, 0, 0 );
55                 *text = "no objectclass attribute";
56                 return oldattrs != NULL
57                         ? LDAP_OBJECT_CLASS_VIOLATION
58                         : LDAP_NO_OBJECT_CLASS_MODS;
59         }
60
61         ret = LDAP_SUCCESS;
62
63         /* check that the entry has required attrs for each oc */
64         for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
65                 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
66                         Debug( LDAP_DEBUG_ANY,
67                                 "entry_check_schema(%s): objectclass \"%s\" not defined\n",
68                                 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
69
70                 } else {
71                         char *s = oc_check_required( e, aoc->a_vals[i] );
72
73                         if (s != NULL) {
74                                 Debug( LDAP_DEBUG_ANY,
75                                         "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
76                                         e->e_dn, aoc->a_vals[i]->bv_val, s );
77                                 *text = "missing required attribute";
78                                 ret = LDAP_OBJECT_CLASS_VIOLATION;
79                                 break;
80                         }
81                 }
82         }
83
84         if ( ret != LDAP_SUCCESS ) {
85             return ret;
86         }
87
88         /* check that each attr in the entry is allowed by some oc */
89         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
90 #ifdef SLAPD_SCHEMA_NOT_COMPAT
91                 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
92 #else
93                 ret = oc_check_allowed( a->a_type, aoc->a_vals );
94 #endif
95                 if ( ret != 0 ) {
96 #ifdef SLAPD_SCHEMA_NOT_COMPAT
97                         char *type = a->a_desc->ad_cname->bv_val;
98 #else
99                         char *type = a->a_type;
100 #endif
101                         Debug( LDAP_DEBUG_ANY,
102                             "Entry (%s), attr \"%s\" not allowed\n",
103                             e->e_dn, type, 0 );
104                         *text = "attribute not allowed";
105                         break;
106                 }
107         }
108
109         return( ret );
110 }
111
112 static char *
113 oc_check_required( Entry *e, struct berval *ocname )
114 {
115         ObjectClass     *oc;
116         AttributeType   *at;
117         int             i;
118         Attribute       *a;
119
120         Debug( LDAP_DEBUG_TRACE,
121                "oc_check_required entry (%s), objectclass \"%s\"\n",
122                e->e_dn, ocname->bv_val, 0 );
123
124         /* find global oc defn. it we don't know about it assume it's ok */
125         if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
126                 return NULL;
127         }
128
129         /* check for empty oc_required */
130         if(oc->soc_required == NULL) {
131                 return NULL;
132         }
133
134         /* for each required attribute */
135         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
136                 at = oc->soc_required[i];
137                 /* see if it's in the entry */
138                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
139 #ifdef SLAPD_SCHEMA_NOT_COMPAT
140                         if( a->a_desc->ad_type == at ) {
141                                 break;
142                         }
143 #else
144                         char            **pp;
145
146                         if ( at->sat_oid &&
147                              strcmp( a->a_type, at->sat_oid ) == 0 ) {
148                                 break;
149                         }
150                         pp = at->sat_names;
151                         if ( pp  == NULL ) {
152                                 /* Empty name list => not found */
153                                 a = NULL;
154                                 break;
155                         }
156                         while ( *pp ) {
157                                 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
158                                         break;
159                                 }
160                                 pp++;
161                         }
162                         if ( *pp ) {
163                                 break;
164                         }
165 #endif
166                 }
167                 /* not there => schema violation */
168                 if ( a == NULL ) {
169 #ifdef SLAPD_SCHEMA_NOT_COMPAT
170                         return at->sat_cname;
171 #else
172                         if ( at->sat_names && at->sat_names[0] ) {
173                                 return at->sat_names[0];
174                         } else {
175                                 return at->sat_oid;
176                         }
177 #endif
178                 }
179         }
180
181         return( NULL );
182 }
183
184 static int
185 oc_check_allowed(
186 #ifdef SLAPD_SCHEMA_NOT_COMPAT
187         AttributeType *at,
188 #else
189         char *type,
190 #endif
191         struct berval **ocl )
192 {
193         ObjectClass     *oc;
194         int             i, j;
195
196 #ifdef SLAPD_SCHEMA_NOT_COMPAT
197         Debug( LDAP_DEBUG_TRACE,
198                 "oc_check_allowed type \"%s\"\n",
199                 at->sat_cname, 0, 0 );
200
201         /* always allow objectclass attribute */
202         if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
203                 return LDAP_SUCCESS;
204         }
205
206 #else
207         AttributeType   *at;
208         char            **pp;
209         char            *p;
210         char            *t;
211
212         Debug( LDAP_DEBUG_TRACE,
213                "oc_check_allowed type \"%s\"\n", type, 0, 0 );
214
215         /* always allow objectclass attribute */
216         if ( strcasecmp( type, "objectclass" ) == 0 ) {
217                 return LDAP_SUCCESS;
218         }
219 #endif
220
221 #ifdef SLAPD_SCHEMA_NOT_COMPAT
222         /*
223          * All operational attributions are allowed by schema rules.
224          */
225         if( is_at_operational(at) ) {
226                 return LDAP_SUCCESS;
227         }
228 #else
229         /*
230          * The "type" we have received is actually an AttributeDescription.
231          * Let's find out the corresponding type.
232          */
233         p = strchr( type, ';' );
234         if ( p ) {
235                 t = ch_malloc( p-type+1 );
236                 strncpy( t, type, p-type );
237                 t[p-type] = '\0';
238                 Debug( LDAP_DEBUG_TRACE,
239                        "oc_check_allowed type \"%s\" from \"%s\"\n",
240                        t, type, 0 );
241
242         } else
243         {
244                 t = type;
245         }
246
247         /*
248          * All operational attributions are allowed by schema rules.
249          */
250         if ( oc_check_op_attr( t ) ) {
251                 return LDAP_SUCCESS;
252         }
253 #endif
254
255         /* check that the type appears as req or opt in at least one oc */
256         for ( i = 0; ocl[i] != NULL; i++ ) {
257                 /* if we know about the oc */
258                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
259                         /* does it require the type? */
260                         for ( j = 0; oc->soc_required != NULL && 
261                                 oc->soc_required[j] != NULL; j++ )
262                         {
263 #ifdef SLAPD_SCHEMA_NOT_COMPAT
264                                 if( at == oc->soc_required[j] ) {
265                                         return LDAP_SUCCESS;
266                                 }
267 #else
268                                 at = oc->soc_required[j];
269                                 if ( at->sat_oid &&
270                                      strcmp(at->sat_oid, t ) == 0 ) {
271                                         if ( t != type )
272                                                 ldap_memfree( t );
273                                         return LDAP_SUCCESS;
274                                 }
275                                 pp = at->sat_names;
276                                 if ( pp == NULL )
277                                         continue;
278                                 while ( *pp ) {
279                                         if ( strcasecmp( *pp, t ) == 0 ) {
280                                                 if ( t != type )
281                                                         ldap_memfree( t );
282                                                 return LDAP_SUCCESS;
283                                         }
284                                         pp++;
285                                 }
286 #endif
287                         }
288                         /* does it allow the type? */
289                         for ( j = 0; oc->soc_allowed != NULL && 
290                                 oc->soc_allowed[j] != NULL; j++ )
291                         {
292 #ifdef SLAPD_SCHEMA_NOT_COMPAT
293                                 if( at == oc->soc_allowed[j] ) {
294                                         return LDAP_SUCCESS;
295                                 }
296 #else
297                                 at = oc->soc_allowed[j];
298                                 if ( at->sat_oid &&
299                                      strcmp( at->sat_oid, t ) == 0 ) {
300                                         if ( t != type )
301                                                 ldap_memfree( t );
302                                         return LDAP_SUCCESS;
303                                 }
304                                 pp = at->sat_names;
305                                 if ( pp == NULL )
306                                         continue;
307                                 while ( *pp ) {
308                                         if ( strcasecmp( *pp, t ) == 0 ||
309                                              strcmp( *pp, "*" ) == 0 ) {
310                                                 if ( t != type )
311                                                         ldap_memfree( t );
312                                                 return LDAP_SUCCESS;
313                                         }
314                                         pp++;
315                                 }
316 #endif
317                         }
318                         /* maybe the next oc allows it */
319
320 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
321                 /* we don't know about the oc. assume it allows it */
322                 } else {
323                         if ( t != type )
324                                 ldap_memfree( t );
325                         return LDAP_SUCCESS;
326 #endif
327                 }
328         }
329
330 #ifndef SLAPD_SCHEMA_NOT_COMPAT
331         if ( t != type )
332                 ldap_memfree( t );
333 #endif
334
335         /* not allowed by any oc */
336         return LDAP_OBJECT_CLASS_VIOLATION;
337 }