]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
0d54333a79db28c5322b8dfb71beda4e411cded6
[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                 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
44                     e->e_dn, 0, 0 );
45                 *text = "no objectclass attribute";
46                 return oldattrs != NULL
47                         ? LDAP_OBJECT_CLASS_VIOLATION
48                         : LDAP_NO_OBJECT_CLASS_MODS;
49         }
50
51         ret = LDAP_SUCCESS;
52
53         /* check that the entry has required attrs for each oc */
54         for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
55                 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
56                         Debug( LDAP_DEBUG_ANY,
57                                 "entry_check_schema(%s): objectclass \"%s\" not defined\n",
58                                 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
59
60                 } else {
61                         char *s = oc_check_required( e, aoc->a_vals[i] );
62
63                         if (s != NULL) {
64                                 Debug( LDAP_DEBUG_ANY,
65                                         "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
66                                         e->e_dn, aoc->a_vals[i]->bv_val, s );
67                                 *text = "missing required attribute";
68                                 ret = LDAP_OBJECT_CLASS_VIOLATION;
69                                 break;
70                         }
71
72                         if( oc == slap_schema.si_oc_extensibleObject )
73                         {
74                                 extensible=1;
75                         }
76
77                 }
78         }
79
80         if ( ret != LDAP_SUCCESS ) {
81             return ret;
82         }
83
84         if( extensible ) {
85                 return LDAP_SUCCESS;
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                 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
91                 if ( ret != 0 ) {
92                         char *type = a->a_desc->ad_cname->bv_val;
93                         Debug( LDAP_DEBUG_ANY,
94                             "Entry (%s), attr \"%s\" not allowed\n",
95                             e->e_dn, type, 0 );
96                         *text = "attribute not allowed";
97                         break;
98                 }
99         }
100
101         return( ret );
102 }
103
104 static char *
105 oc_check_required( Entry *e, struct berval *ocname )
106 {
107         ObjectClass     *oc;
108         AttributeType   *at;
109         int             i;
110         Attribute       *a;
111
112         Debug( LDAP_DEBUG_TRACE,
113                "oc_check_required entry (%s), objectclass \"%s\"\n",
114                e->e_dn, ocname->bv_val, 0 );
115
116         /* find global oc defn. it we don't know about it assume it's ok */
117         if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
118                 return NULL;
119         }
120
121         /* check for empty oc_required */
122         if(oc->soc_required == NULL) {
123                 return NULL;
124         }
125
126         /* for each required attribute */
127         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
128                 at = oc->soc_required[i];
129                 /* see if it's in the entry */
130                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
131                         if( a->a_desc->ad_type == at ) {
132                                 break;
133                         }
134                 }
135                 /* not there => schema violation */
136                 if ( a == NULL ) {
137                         return at->sat_cname;
138                 }
139         }
140
141         return( NULL );
142 }
143
144 int oc_check_allowed(
145         AttributeType *at,
146         struct berval **ocl )
147 {
148         ObjectClass     *oc;
149         int             i, j;
150
151         Debug( LDAP_DEBUG_TRACE,
152                 "oc_check_allowed type \"%s\"\n",
153                 at->sat_cname, 0, 0 );
154
155         /* always allow objectclass attribute */
156         if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
157                 return LDAP_SUCCESS;
158         }
159
160
161         /*
162          * All operational attributions are allowed by schema rules.
163          */
164         if( is_at_operational(at) ) {
165                 return LDAP_SUCCESS;
166         }
167
168         /* check that the type appears as req or opt in at least one oc */
169         for ( i = 0; ocl[i] != NULL; i++ ) {
170                 /* if we know about the oc */
171                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
172                         /* does it require the type? */
173                         for ( j = 0; oc->soc_required != NULL && 
174                                 oc->soc_required[j] != NULL; j++ )
175                         {
176                                 if( at == oc->soc_required[j] ) {
177                                         return LDAP_SUCCESS;
178                                 }
179                         }
180                         /* does it allow the type? */
181                         for ( j = 0; oc->soc_allowed != NULL && 
182                                 oc->soc_allowed[j] != NULL; j++ )
183                         {
184                                 if( at == oc->soc_allowed[j] ) {
185                                         return LDAP_SUCCESS;
186                                 }
187                         }
188                         /* maybe the next oc allows it */
189
190 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
191                 /* we don't know about the oc. assume it allows it */
192                 } else {
193                         if ( t != type )
194                                 ldap_memfree( t );
195                         return LDAP_SUCCESS;
196 #endif
197                 }
198         }
199
200
201         /* not allowed by any oc */
202         return LDAP_OBJECT_CLASS_VIOLATION;
203 }