]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
Fix backend_destroy to call bi_destroy instead of bi_close
[openldap] / servers / slapd / schema.c
1 /* schema.c - routines to enforce schema definitions */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11
12 static struct objclass  *oc_find(char *ocname);
13 static char *   oc_check_required(Entry *e, char *ocname);
14 static int              oc_check_allowed(char *type, struct berval **ocl);
15
16 /*
17  * oc_check - check that entry e conforms to the schema required by
18  * its object class(es). returns 0 if so, non-zero otherwise.
19  */
20
21 int
22 oc_schema_check( Entry *e )
23 {
24         Attribute       *a, *aoc;
25         struct objclass *oc;
26         int             i;
27         int             ret = 0;
28
29         /* find the object class attribute - could error out here */
30         if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
31                 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
32                     e->e_dn, 0, 0 );
33                 return( 0 );
34         }
35
36         /* check that the entry has required attrs for each oc */
37         for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
38                 char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
39
40                 if (s != NULL) {
41                         Debug( LDAP_DEBUG_ANY,
42                             "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
43                             e->e_dn, aoc->a_vals[i]->bv_val, s );
44                         ret = 1;
45                 }
46         }
47
48         if ( ret != 0 ) {
49             return( ret );
50         }
51
52         /* check that each attr in the entry is allowed by some oc */
53         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
54                 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
55                         Debug( LDAP_DEBUG_ANY,
56                             "Entry (%s), attr \"%s\" not allowed\n",
57                             e->e_dn, a->a_type, 0 );
58                         ret = 1;
59                 }
60         }
61
62         return( ret );
63 }
64
65 static char *
66 oc_check_required( Entry *e, char *ocname )
67 {
68         struct objclass *oc;
69         int             i;
70         Attribute       *a;
71
72         /* find global oc defn. it we don't know about it assume it's ok */
73         if ( (oc = oc_find( ocname )) == NULL ) {
74                 return( 0 );
75         }
76
77         /* check for empty oc_required */
78         if(oc->oc_required == NULL) {
79                 return( 0 );
80         }
81
82         /* for each required attribute */
83         for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
84                 /* see if it's in the entry */
85                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
86                         if ( strcasecmp( a->a_type, oc->oc_required[i] )
87                             == 0 ) {
88                                 break;
89                         }
90                 }
91
92                 /* not there => schema violation */
93                 if ( a == NULL ) {
94                         return oc->oc_required[i];
95                 }
96         }
97
98         return( NULL );
99 }
100
101 /*
102  * check to see if attribute is 'operational' or not.
103  * this function should be externalized...
104  */
105 static int
106 oc_check_operational( char *type )
107 {
108         return ( strcasecmp( type, "modifiersname" ) == 0 ||
109                 strcasecmp( type, "modifytimestamp" ) == 0 ||
110                 strcasecmp( type, "creatorsname" ) == 0 ||
111                 strcasecmp( type, "createtimestamp" ) == 0 )
112                 ? 1 : 0;
113 }
114
115 static int
116 oc_check_allowed( char *type, struct berval **ocl )
117 {
118         struct objclass *oc;
119         int             i, j;
120
121         /* always allow objectclass attribute */
122         if ( strcasecmp( type, "objectclass" ) == 0 ) {
123                 return( 0 );
124         }
125
126         if ( oc_check_operational( type ) ) {
127                 return( 0 );
128         }
129
130         /* check that the type appears as req or opt in at least one oc */
131         for ( i = 0; ocl[i] != NULL; i++ ) {
132                 /* if we know about the oc */
133                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
134                         /* does it require the type? */
135                         for ( j = 0; oc->oc_required != NULL && 
136                                 oc->oc_required[j] != NULL; j++ ) {
137                                 if ( strcasecmp( oc->oc_required[j], type )
138                                     == 0 ) {
139                                         return( 0 );
140                                 }
141                         }
142                         /* does it allow the type? */
143                         for ( j = 0; oc->oc_allowed != NULL && 
144                                 oc->oc_allowed[j] != NULL; j++ ) {
145                                 if ( strcasecmp( oc->oc_allowed[j], type )
146                                     == 0 || strcmp( oc->oc_allowed[j], "*" )
147                                     == 0 )
148                                 {
149                                         return( 0 );
150                                 }
151                         }
152                         /* maybe the next oc allows it */
153
154                 /* we don't know about the oc. assume it allows it */
155                 } else {
156                         return( 0 );
157                 }
158         }
159
160         /* not allowed by any oc */
161         return( 1 );
162 }
163
164 static struct objclass *
165 oc_find( char *ocname )
166 {
167         struct objclass *oc;
168
169         for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
170                 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
171                         return( oc );
172                 }
173         }
174
175         return( NULL );
176 }
177
178 #ifdef LDAP_DEBUG
179
180 static void
181 oc_print( struct objclass *oc )
182 {
183         int     i;
184
185         printf( "objectclass %s\n", oc->oc_name );
186         if ( oc->oc_required != NULL ) {
187                 printf( "\trequires %s", oc->oc_required[0] );
188                 for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
189                         printf( ",%s", oc->oc_required[i] );
190                 }
191                 printf( "\n" );
192         }
193         if ( oc->oc_allowed != NULL ) {
194                 printf( "\tallows %s", oc->oc_allowed[0] );
195                 for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
196                         printf( ",%s", oc->oc_allowed[i] );
197                 }
198                 printf( "\n" );
199         }
200 }
201
202 #endif