]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
debug messages incorrectly said "add" instead of "remove".
[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 int              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                 if ( oc_check_required( e, aoc->a_vals[i]->bv_val ) != 0 ) {
39                         Debug( LDAP_DEBUG_ANY,
40                             "Entry (%s), required attr (%s) missing\n",
41                             e->e_dn, aoc->a_vals[i]->bv_val, 0 );
42                         ret = 1;
43                 }
44         }
45
46         if ( ret != 0 ) {
47             return( ret );
48         }
49
50         /* check that each attr in the entry is allowed by some oc */
51         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
52                 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
53                         Debug( LDAP_DEBUG_ANY,
54                             "Entry (%s), attr (%s) not allowed\n",
55                             e->e_dn, a->a_type, 0 );
56                         ret = 1;
57                 }
58         }
59
60         return( ret );
61 }
62
63 static int
64 oc_check_required( Entry *e, char *ocname )
65 {
66         struct objclass *oc;
67         int             i;
68         Attribute       *a;
69
70         /* find global oc defn. it we don't know about it assume it's ok */
71         if ( (oc = oc_find( ocname )) == NULL ) {
72                 return( 0 );
73         }
74
75         /* check for empty oc_required */
76         if(oc->oc_required == NULL) {
77                 return( 0 );
78         }
79
80         /* for each required attribute */
81         for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
82                 /* see if it's in the entry */
83                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
84                         if ( strcasecmp( a->a_type, oc->oc_required[i] )
85                             == 0 ) {
86                                 break;
87                         }
88                 }
89
90                 /* not there => schema violation */
91                 if ( a == NULL ) {
92                         return( 1 );
93                 }
94         }
95
96         return( 0 );
97 }
98
99 static int
100 oc_check_allowed( char *type, struct berval **ocl )
101 {
102         struct objclass *oc;
103         int             i, j;
104
105         /* always allow objectclass attribute */
106         if ( strcasecmp( type, "objectclass" ) == 0 ) {
107                 return( 0 );
108         }
109
110         /* check that the type appears as req or opt in at least one oc */
111         for ( i = 0; ocl[i] != NULL; i++ ) {
112                 /* if we know about the oc */
113                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
114                         /* does it require the type? */
115                         for ( j = 0; oc->oc_required != NULL && 
116                                 oc->oc_required[j] != NULL; j++ ) {
117                                 if ( strcasecmp( oc->oc_required[j], type )
118                                     == 0 ) {
119                                         return( 0 );
120                                 }
121                         }
122                         /* does it allow the type? */
123                         for ( j = 0; oc->oc_allowed != NULL && 
124                                 oc->oc_allowed[j] != NULL; j++ ) {
125                                 if ( strcasecmp( oc->oc_allowed[j], type )
126                                     == 0 || strcmp( oc->oc_allowed[j], "*" )
127                                     == 0 )
128                                 {
129                                         return( 0 );
130                                 }
131                         }
132                         /* maybe the next oc allows it */
133
134                 /* we don't know about the oc. assume it allows it */
135                 } else {
136                         return( 0 );
137                 }
138         }
139
140         /* not allowed by any oc */
141         return( 1 );
142 }
143
144 static struct objclass *
145 oc_find( char *ocname )
146 {
147         struct objclass *oc;
148
149         for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
150                 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
151                         return( oc );
152                 }
153         }
154
155         return( NULL );
156 }
157
158 #ifdef LDAP_DEBUG
159
160 static void
161 oc_print( struct objclass *oc )
162 {
163         int     i;
164
165         printf( "objectclass %s\n", oc->oc_name );
166         if ( oc->oc_required != NULL ) {
167                 printf( "\trequires %s", oc->oc_required[0] );
168                 for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
169                         printf( ",%s", oc->oc_required[i] );
170                 }
171                 printf( "\n" );
172         }
173         if ( oc->oc_allowed != NULL ) {
174                 printf( "\tallows %s", oc->oc_allowed[0] );
175                 for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
176                         printf( ",%s", oc->oc_allowed[i] );
177                 }
178                 printf( "\n" );
179         }
180 }
181
182 #endif