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