]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
Move backend_syncfreq code down into back-ldbm. Creates new configuration
[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         return( ret );
137 }
138
139 static char *
140 oc_check_required( Entry *e, struct berval *ocname )
141 {
142         ObjectClass     *oc;
143         AttributeType   *at;
144         int             i;
145         Attribute       *a;
146
147 #ifdef NEW_LOGGING
148         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
149                    "oc_check_required: dn (%s), objectClass \"%s\"\n",
150                    e->e_dn, ocname->bv_val ));
151 #else
152         Debug( LDAP_DEBUG_TRACE,
153                "oc_check_required entry (%s), objectClass \"%s\"\n",
154                e->e_dn, ocname->bv_val, 0 );
155 #endif
156
157
158         /* find global oc defn. it we don't know about it assume it's ok */
159         if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
160                 return NULL;
161         }
162
163         /* check for empty oc_required */
164         if(oc->soc_required == NULL) {
165                 return NULL;
166         }
167
168         /* for each required attribute */
169         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
170                 at = oc->soc_required[i];
171                 /* see if it's in the entry */
172                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
173                         if( a->a_desc->ad_type == at ) {
174                                 break;
175                         }
176                 }
177                 /* not there => schema violation */
178                 if ( a == NULL ) {
179                         return at->sat_cname;
180                 }
181         }
182
183         return( NULL );
184 }
185
186 int oc_check_allowed(
187         AttributeType *at,
188         struct berval **ocl )
189 {
190         ObjectClass     *oc;
191         int             i, j;
192
193 #ifdef NEW_LOGGING
194         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
195                    "oc_check_allowed: type \"%s\"\n", at->sat_cname ));
196 #else
197         Debug( LDAP_DEBUG_TRACE,
198                 "oc_check_allowed type \"%s\"\n",
199                 at->sat_cname, 0, 0 );
200 #endif
201
202         /* always allow objectClass attribute */
203         if ( strcasecmp( at->sat_cname, "objectClass" ) == 0 ) {
204                 return LDAP_SUCCESS;
205         }
206
207
208         /*
209          * All operational attributions are allowed by schema rules.
210          */
211         if( is_at_operational(at) ) {
212                 return LDAP_SUCCESS;
213         }
214
215         /* check that the type appears as req or opt in at least one oc */
216         for ( i = 0; ocl[i] != NULL; i++ ) {
217                 /* if we know about the oc */
218                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
219                         /* does it require the type? */
220                         for ( j = 0; oc->soc_required != NULL && 
221                                 oc->soc_required[j] != NULL; j++ )
222                         {
223                                 if( at == oc->soc_required[j] ) {
224                                         return LDAP_SUCCESS;
225                                 }
226                         }
227                         /* does it allow the type? */
228                         for ( j = 0; oc->soc_allowed != NULL && 
229                                 oc->soc_allowed[j] != NULL; j++ )
230                         {
231                                 if( at == oc->soc_allowed[j] ) {
232                                         return LDAP_SUCCESS;
233                                 }
234                         }
235                         /* maybe the next oc allows it */
236                 }
237         }
238
239         /* not allowed by any oc */
240         return LDAP_OBJECT_CLASS_VIOLATION;
241 }