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