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