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