]> git.sur5r.net Git - openldap/blob - servers/slapd/cr.c
Remove casts of AVL function pointers.
[openldap] / servers / slapd / cr.c
1 /* cr.c - content rule routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 #ifdef SLAP_EXTENDED_SCHEMA
20
21 struct cindexrec {
22         struct berval   cir_name;
23         ContentRule     *cir_cr;
24 };
25
26 static Avlnode  *cr_index = NULL;
27 static ContentRule *cr_list = NULL;
28
29 static int
30 cr_index_cmp(
31     const void  *v_cir1,
32     const void  *v_cir2 )
33 {
34         const struct cindexrec  *cir1 = v_cir1;
35         const struct cindexrec  *cir2 = v_cir2;
36         int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
37         if (i)
38                 return i;
39         return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
40 }
41
42 static int
43 cr_index_name_cmp(
44     const void  *v_name,
45     const void  *v_cir )
46 {
47         const struct berval    *name = v_name;
48         const struct cindexrec *cir  = v_cir;
49         int i = name->bv_len - cir->cir_name.bv_len;
50         if (i)
51                 return i;
52         return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
53 }
54
55 ContentRule *
56 cr_find( const char *crname )
57 {
58         struct berval bv;
59
60         bv.bv_val = (char *)crname;
61         bv.bv_len = strlen( crname );
62
63         return( cr_bvfind( &bv ) );
64 }
65
66 ContentRule *
67 cr_bvfind( struct berval *crname )
68 {
69         struct cindexrec        *cir;
70
71         cir = avl_find( cr_index, crname, cr_index_name_cmp );
72
73         if ( cir != NULL ) {
74                 return( cir->cir_cr );
75         }
76
77         return( NULL );
78 }
79
80 void
81 cr_destroy( void )
82 {
83         ContentRule *c, *n;
84
85         avl_free(cr_index, ldap_memfree);
86         for (c=cr_list; c; c=n)
87         {
88                 n = c->scr_next;
89                 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
90                 if (c->scr_required) ldap_memfree(c->scr_required);
91                 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
92                 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
93                 ldap_contentrule_free((LDAPContentRule *)c);
94         }
95 }
96
97 static int
98 cr_insert(
99     ContentRule         *scr,
100     const char          **err
101 )
102 {
103         ContentRule     **crp;
104         struct cindexrec        *cir;
105         char                    **names;
106
107         crp = &cr_list;
108         while ( *crp != NULL ) {
109                 crp = &(*crp)->scr_next;
110         }
111         *crp = scr;
112
113         if ( scr->scr_oid ) {
114                 cir = (struct cindexrec *)
115                         ch_calloc( 1, sizeof(struct cindexrec) );
116                 cir->cir_name.bv_val = scr->scr_oid;
117                 cir->cir_name.bv_len = strlen( scr->scr_oid );
118                 cir->cir_cr = scr;
119
120                 assert( cir->cir_name.bv_val );
121                 assert( cir->cir_cr );
122
123                 if ( avl_insert( &cr_index, (caddr_t) cir,
124                                  cr_index_cmp, avl_dup_error ) )
125                 {
126                         *err = scr->scr_oid;
127                         ldap_memfree(cir);
128                         return SLAP_SCHERR_CR_DUP;
129                 }
130
131                 /* FIX: temporal consistency check */
132                 assert( cr_bvfind(&cir->cir_name) != NULL );
133         }
134
135         if ( (names = scr->scr_names) ) {
136                 while ( *names ) {
137                         cir = (struct cindexrec *)
138                                 ch_calloc( 1, sizeof(struct cindexrec) );
139                         cir->cir_name.bv_val = *names;
140                         cir->cir_name.bv_len = strlen( *names );
141                         cir->cir_cr = scr;
142
143                         assert( cir->cir_name.bv_val );
144                         assert( cir->cir_cr );
145
146                         if ( avl_insert( &cr_index, (caddr_t) cir,
147                                          cr_index_cmp, avl_dup_error ) )
148                         {
149                                 *err = *names;
150                                 ldap_memfree(cir);
151                                 return SLAP_SCHERR_CR_DUP;
152                         }
153
154                         /* FIX: temporal consistency check */
155                         assert( cr_bvfind(&cir->cir_name) != NULL );
156
157                         names++;
158                 }
159         }
160
161         return 0;
162 }
163
164 static int
165 cr_add_auxiliaries(
166     ContentRule         *scr,
167         int                     *op,
168     const char          **err )
169 {
170         int naux;
171
172         if( scr->scr_oc_oids_aux == NULL ) return 0;
173         
174         for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
175                 /* count them */ ;
176         }
177
178         scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
179
180         for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
181                 ObjectClass *soc = scr->scr_auxiliaries[naux]
182                         = oc_find(scr->scr_oc_oids_aux[naux]);
183                 if ( !soc ) {
184                         *err = scr->scr_oc_oids_aux[naux];
185                         return SLAP_SCHERR_CLASS_NOT_FOUND;
186                 }
187
188                 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
189
190                 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
191                         *err = scr->scr_oc_oids_aux[naux];
192                         return SLAP_SCHERR_CR_BAD_AUX;
193                 }
194         }
195
196         scr->scr_auxiliaries[naux] = NULL;
197
198         return 0;
199 }
200
201 static int
202 cr_create_required(
203     ContentRule         *scr,
204         int                     *op,
205     const char          **err )
206 {
207     char                **attrs = scr->scr_at_oids_must;
208         char            **attrs1;
209         AttributeType   *sat;
210
211         if ( attrs ) {
212                 attrs1 = attrs;
213                 while ( *attrs1 ) {
214                         sat = at_find(*attrs1);
215                         if ( !sat ) {
216                                 *err = *attrs1;
217                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
218                         }
219
220                         if( is_at_operational( sat )) (*op)++;
221
222                         if ( at_find_in_list(sat, scr->scr_required) < 0) {
223                                 if ( at_append_to_list(sat, &scr->scr_required) ) {
224                                         *err = *attrs1;
225                                         return SLAP_SCHERR_OUTOFMEM;
226                                 }
227                         } else {
228                                 *err = *attrs1;
229                                 return SLAP_SCHERR_CR_BAD_AT;
230                         }
231                         attrs1++;
232                 }
233         }
234         return 0;
235 }
236
237 static int
238 cr_create_allowed(
239     ContentRule         *scr,
240         int                     *op,
241     const char          **err )
242 {
243     char                **attrs = scr->scr_at_oids_may;
244         char            **attrs1;
245         AttributeType   *sat;
246
247         if ( attrs ) {
248                 attrs1 = attrs;
249                 while ( *attrs1 ) {
250                         sat = at_find(*attrs1);
251                         if ( !sat ) {
252                                 *err = *attrs1;
253                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
254                         }
255
256                         if( is_at_operational( sat )) (*op)++;
257
258                         if ( at_find_in_list(sat, scr->scr_required) < 0 &&
259                                 at_find_in_list(sat, scr->scr_allowed) < 0 )
260                         {
261                                 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
262                                         *err = *attrs1;
263                                         return SLAP_SCHERR_OUTOFMEM;
264                                 }
265                         } else {
266                                 *err = *attrs1;
267                                 return SLAP_SCHERR_CR_BAD_AT;
268                         }
269                         attrs1++;
270                 }
271         }
272         return 0;
273 }
274
275 static int
276 cr_create_precluded(
277     ContentRule         *scr,
278         int                     *op,
279     const char          **err )
280 {
281     char                **attrs = scr->scr_at_oids_not;
282         char            **attrs1;
283         AttributeType   *sat;
284
285         if ( attrs ) {
286                 attrs1 = attrs;
287                 while ( *attrs1 ) {
288                         sat = at_find(*attrs1);
289                         if ( !sat ) {
290                                 *err = *attrs1;
291                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
292                         }
293
294                         if( is_at_operational( sat )) (*op)++;
295
296                         /* FIXME: should also make sure attribute type is not
297                                 a required attribute of the structural class or
298                                 any auxiliary class */
299                         if ( at_find_in_list(sat, scr->scr_required) < 0 &&
300                                 at_find_in_list(sat, scr->scr_allowed) < 0 &&
301                                 at_find_in_list(sat, scr->scr_precluded) < 0 )
302                         {
303                                 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
304                                         *err = *attrs1;
305                                         return SLAP_SCHERR_OUTOFMEM;
306                                 }
307                         } else {
308                                 *err = *attrs1;
309                                 return SLAP_SCHERR_CR_BAD_AT;
310                         }
311                         attrs1++;
312                 }
313         }
314         return 0;
315 }
316
317 int
318 cr_add(
319     LDAPContentRule     *cr,
320         int user,
321     const char          **err
322 )
323 {
324         ContentRule     *scr;
325         int             code;
326         int             op = 0;
327
328         if ( cr->cr_names != NULL ) {
329                 int i;
330
331                 for( i=0; cr->cr_names[i]; i++ ) {
332                         if( !slap_valid_descr( cr->cr_names[i] ) ) {
333                                 return SLAP_SCHERR_BAD_DESCR;
334                         }
335                 }
336         }
337
338         if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
339                 /* Expand OID macros */
340                 char *oid = oidm_find( cr->cr_oid );
341                 if ( !oid ) {
342                         *err = cr->cr_oid;
343                         return SLAP_SCHERR_OIDM;
344                 }
345                 if ( oid != cr->cr_oid ) {
346                         ldap_memfree( cr->cr_oid );
347                         cr->cr_oid = oid;
348                 }
349         }
350
351         scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
352         AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
353
354         scr->scr_sclass = oc_find(cr->cr_oid);
355         if ( !scr->scr_sclass ) {
356                 *err = cr->cr_oid;
357                 return SLAP_SCHERR_CLASS_NOT_FOUND;
358         }
359
360         /* check object class usage */
361         if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
362         {
363                 *err = cr->cr_oid;
364                 return SLAP_SCHERR_CR_BAD_STRUCT;
365         }
366
367         if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
368
369         code = cr_add_auxiliaries( scr, &op, err );
370         if ( code != 0 ) return code;
371
372         code = cr_create_required( scr, &op, err );
373         if ( code != 0 ) return code;
374
375         code = cr_create_allowed( scr, &op, err );
376         if ( code != 0 ) return code;
377
378         code = cr_create_precluded( scr, &op, err );
379         if ( code != 0 ) return code;
380
381         if( user && op ) return SLAP_SCHERR_CR_BAD_AUX;
382
383         code = cr_insert(scr,err);
384         return code;
385 }
386
387 #endif
388
389 int
390 cr_schema_info( Entry *e )
391 {
392 #ifdef SLAP_EXTENDED_SCHEMA
393         struct berval   vals[2];
394         ContentRule     *cr;
395
396         AttributeDescription *ad_ditContentRules = slap_schema.si_ad_ditContentRules;
397
398         vals[1].bv_val = NULL;
399
400         for ( cr = cr_list; cr; cr = cr->scr_next ) {
401                 if ( ldap_contentrule2bv( &cr->scr_crule, vals ) == NULL ) {
402                         return -1;
403                 }
404
405 #if 0
406                 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
407 #endif
408
409 #if 0
410                 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
411                (long) vals[0].bv_len, vals[0].bv_val, 0 );
412 #endif
413                 if( attr_merge( e, ad_ditContentRules, vals ) )
414                         return -1;
415                 ldap_memfree( vals[0].bv_val );
416         }
417 #endif
418         return 0;
419 }