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