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