]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
fix for select_backend suggested G. Gombas (ITS 1090)
[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 static char *   oc_check_required(Entry *e, struct berval *ocname);
20
21 /*
22  * entry_schema_check - check that entry e conforms to the schema required
23  * by its object class(es).
24  *
25  * returns 0 if so, non-zero otherwise.
26  */
27
28 int
29 entry_schema_check( 
30         Entry *e, Attribute *oldattrs, const char** text )
31 {
32         Attribute       *a, *aoc;
33         ObjectClass *oc;
34         int             i;
35         int             ret;
36         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
37         int extensible = 0;
38
39         if( !global_schemacheck ) return LDAP_SUCCESS;
40
41         /* find the object class attribute - could error out here */
42         if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
43 #ifdef NEW_LOGGING
44                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
45                            "entry_schema_check: No objectClass for entry (%s).\n", e->e_dn ));
46 #else
47                 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
48                     e->e_dn, 0, 0 );
49 #endif
50
51                 *text = "no objectClass attribute";
52                 return LDAP_OBJECT_CLASS_VIOLATION;
53         }
54
55         /* check that the entry has required attrs for each oc */
56         for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
57                 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
58 #ifdef NEW_LOGGING
59                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
60                                    "entry_schema_check: dn (%s), objectClass \"%s\" not recognized\n",
61                                    e->e_dn, aoc->a_vals[i]->bv_val ));
62 #else
63                         Debug( LDAP_DEBUG_ANY,
64                                 "entry_check_schema(%s): objectClass \"%s\" not recognized\n",
65                                 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
66 #endif
67
68                         *text = "unrecognized object class";
69                         return LDAP_OBJECT_CLASS_VIOLATION;
70
71                 } else {
72                         char *s = oc_check_required( e, aoc->a_vals[i] );
73
74                         if (s != NULL) {
75 #ifdef NEW_LOGGING
76                                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
77                                            "entry_schema_check: dn (%s) oc \"%s\" requires att \"%s\"\n",
78                                            e->e_dn, aoc->a_vals[i]->bv_val, s ));
79 #else
80                                 Debug( LDAP_DEBUG_ANY,
81                                         "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
82                                         e->e_dn, aoc->a_vals[i]->bv_val, s );
83 #endif
84
85                                 *text = "missing required attribute";
86                                 return LDAP_OBJECT_CLASS_VIOLATION;
87                         }
88
89                         if( oc == slap_schema.si_oc_extensibleObject ) {
90                                 extensible=1;
91                         }
92
93                 }
94         }
95
96         if( extensible ) {
97                 return LDAP_SUCCESS;
98         }
99
100         /* optimistic */
101         ret = LDAP_SUCCESS;
102
103         /* check that each attr in the entry is allowed by some oc */
104         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
105                 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
106                 if ( ret != 0 ) {
107                         char *type = a->a_desc->ad_cname->bv_val;
108 #ifdef NEW_LOGGING
109                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
110                                    "entry_schema_check: Entry (%s) attr \"%s\" not allowed.\n",
111                                    e->e_dn, type ));
112 #else
113                         Debug( LDAP_DEBUG_ANY,
114                             "Entry (%s), attr \"%s\" not allowed\n",
115                             e->e_dn, type, 0 );
116 #endif
117
118                         *text = "attribute not allowed";
119                         break;
120                 }
121         }
122
123         return( ret );
124 }
125
126 static char *
127 oc_check_required( Entry *e, struct berval *ocname )
128 {
129         ObjectClass     *oc;
130         AttributeType   *at;
131         int             i;
132         Attribute       *a;
133
134 #ifdef NEW_LOGGING
135         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
136                    "oc_check_required: dn (%s), objectClass \"%s\"\n",
137                    e->e_dn, ocname->bv_val ));
138 #else
139         Debug( LDAP_DEBUG_TRACE,
140                "oc_check_required entry (%s), objectClass \"%s\"\n",
141                e->e_dn, ocname->bv_val, 0 );
142 #endif
143
144
145         /* find global oc defn. it we don't know about it assume it's ok */
146         if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
147                 return NULL;
148         }
149
150         /* check for empty oc_required */
151         if(oc->soc_required == NULL) {
152                 return NULL;
153         }
154
155         /* for each required attribute */
156         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
157                 at = oc->soc_required[i];
158                 /* see if it's in the entry */
159                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
160                         if( a->a_desc->ad_type == at ) {
161                                 break;
162                         }
163                 }
164                 /* not there => schema violation */
165                 if ( a == NULL ) {
166                         return at->sat_cname;
167                 }
168         }
169
170         return( NULL );
171 }
172
173 int oc_check_allowed(
174         AttributeType *at,
175         struct berval **ocl )
176 {
177         ObjectClass     *oc;
178         int             i, j;
179
180 #ifdef NEW_LOGGING
181         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
182                    "oc_check_allowed: type \"%s\"\n", at->sat_cname ));
183 #else
184         Debug( LDAP_DEBUG_TRACE,
185                 "oc_check_allowed type \"%s\"\n",
186                 at->sat_cname, 0, 0 );
187 #endif
188
189
190         /* always allow objectClass attribute */
191         if ( strcasecmp( at->sat_cname, "objectClass" ) == 0 ) {
192                 return LDAP_SUCCESS;
193         }
194
195
196         /*
197          * All operational attributions are allowed by schema rules.
198          */
199         if( is_at_operational(at) ) {
200                 return LDAP_SUCCESS;
201         }
202
203         /* check that the type appears as req or opt in at least one oc */
204         for ( i = 0; ocl[i] != NULL; i++ ) {
205                 /* if we know about the oc */
206                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
207                         /* does it require the type? */
208                         for ( j = 0; oc->soc_required != NULL && 
209                                 oc->soc_required[j] != NULL; j++ )
210                         {
211                                 if( at == oc->soc_required[j] ) {
212                                         return LDAP_SUCCESS;
213                                 }
214                         }
215                         /* does it allow the type? */
216                         for ( j = 0; oc->soc_allowed != NULL && 
217                                 oc->soc_allowed[j] != NULL; j++ )
218                         {
219                                 if( at == oc->soc_allowed[j] ) {
220                                         return LDAP_SUCCESS;
221                                 }
222                         }
223                         /* maybe the next oc allows it */
224
225 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
226                 /* we don't know about the oc. assume it allows it */
227                 } else {
228                         if ( t != type )
229                                 ldap_memfree( t );
230                         return LDAP_SUCCESS;
231 #endif
232                 }
233         }
234
235
236         /* not allowed by any oc */
237         return LDAP_OBJECT_CLASS_VIOLATION;
238 }