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