Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / cr.c
blob30af10932b0604b4ac5af82be65c5eeda478e44c
1 /* cr.c - content rule routines */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/cr.c,v 1.22.2.3 2008/02/11 23:26:44 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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>.
17 #include "portable.h"
19 #include <stdio.h>
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
25 #include "slap.h"
27 struct cindexrec {
28 struct berval cir_name;
29 ContentRule *cir_cr;
32 static Avlnode *cr_index = NULL;
33 static LDAP_STAILQ_HEAD(CRList, ContentRule) cr_list
34 = LDAP_STAILQ_HEAD_INITIALIZER(cr_list);
36 static int
37 cr_index_cmp(
38 const void *v_cir1,
39 const void *v_cir2 )
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 );
48 static int
49 cr_index_name_cmp(
50 const void *v_name,
51 const void *v_cir )
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 );
60 ContentRule *
61 cr_find( const char *crname )
63 struct berval bv;
65 bv.bv_val = (char *)crname;
66 bv.bv_len = strlen( crname );
68 return( cr_bvfind( &bv ) );
71 ContentRule *
72 cr_bvfind( struct berval *crname )
74 struct cindexrec *cir;
76 cir = avl_find( cr_index, crname, cr_index_name_cmp );
78 if ( cir != NULL ) {
79 return( cir->cir_cr );
82 return( NULL );
85 static int
86 cr_destroy_one( ContentRule *c )
88 assert( c != NULL );
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);
96 return 0;
99 void
100 cr_destroy( void )
102 ContentRule *c;
104 avl_free(cr_index, ldap_memfree);
106 while( !LDAP_STAILQ_EMPTY(&cr_list) ) {
107 c = LDAP_STAILQ_FIRST(&cr_list);
108 LDAP_STAILQ_REMOVE_HEAD(&cr_list, scr_next);
110 cr_destroy_one( c );
114 static int
115 cr_insert(
116 ContentRule *scr,
117 const char **err
120 struct cindexrec *cir;
121 char **names;
123 if ( scr->scr_oid ) {
124 cir = (struct cindexrec *)
125 ch_calloc( 1, sizeof(struct cindexrec) );
126 cir->cir_name.bv_val = scr->scr_oid;
127 cir->cir_name.bv_len = strlen( scr->scr_oid );
128 cir->cir_cr = scr;
130 assert( cir->cir_name.bv_val != NULL );
131 assert( cir->cir_cr != NULL );
133 if ( avl_insert( &cr_index, (caddr_t) cir,
134 cr_index_cmp, avl_dup_error ) )
136 *err = scr->scr_oid;
137 ldap_memfree(cir);
138 return SLAP_SCHERR_CR_DUP;
141 /* FIX: temporal consistency check */
142 assert( cr_bvfind(&cir->cir_name) != NULL );
145 if ( (names = scr->scr_names) ) {
146 while ( *names ) {
147 cir = (struct cindexrec *)
148 ch_calloc( 1, sizeof(struct cindexrec) );
149 cir->cir_name.bv_val = *names;
150 cir->cir_name.bv_len = strlen( *names );
151 cir->cir_cr = scr;
153 assert( cir->cir_name.bv_val != NULL );
154 assert( cir->cir_cr != NULL );
156 if ( avl_insert( &cr_index, (caddr_t) cir,
157 cr_index_cmp, avl_dup_error ) )
159 *err = *names;
160 ldap_memfree(cir);
161 return SLAP_SCHERR_CR_DUP;
164 /* FIX: temporal consistency check */
165 assert( cr_bvfind(&cir->cir_name) != NULL );
167 names++;
171 LDAP_STAILQ_INSERT_TAIL(&cr_list, scr, scr_next);
173 return 0;
176 static int
177 cr_add_auxiliaries(
178 ContentRule *scr,
179 int *op,
180 const char **err )
182 int naux;
184 if( scr->scr_oc_oids_aux == NULL ) return 0;
186 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
187 /* count them */ ;
190 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
192 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
193 ObjectClass *soc = scr->scr_auxiliaries[naux]
194 = oc_find(scr->scr_oc_oids_aux[naux]);
195 if ( !soc ) {
196 *err = scr->scr_oc_oids_aux[naux];
197 return SLAP_SCHERR_CLASS_NOT_FOUND;
200 if( soc->soc_flags & SLAP_OC_OPERATIONAL &&
201 soc != slap_schema.si_oc_extensibleObject )
203 (*op)++;
206 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
207 *err = scr->scr_oc_oids_aux[naux];
208 return SLAP_SCHERR_CR_BAD_AUX;
212 scr->scr_auxiliaries[naux] = NULL;
213 return 0;
216 static int
217 cr_create_required(
218 ContentRule *scr,
219 int *op,
220 const char **err )
222 char **attrs = scr->scr_at_oids_must;
223 char **attrs1;
224 AttributeType *sat;
226 if ( attrs ) {
227 attrs1 = attrs;
228 while ( *attrs1 ) {
229 sat = at_find(*attrs1);
230 if ( !sat ) {
231 *err = *attrs1;
232 return SLAP_SCHERR_ATTR_NOT_FOUND;
235 if( is_at_operational( sat )) (*op)++;
237 if ( at_find_in_list(sat, scr->scr_required) < 0) {
238 if ( at_append_to_list(sat, &scr->scr_required) ) {
239 *err = *attrs1;
240 return SLAP_SCHERR_OUTOFMEM;
242 } else {
243 *err = *attrs1;
244 return SLAP_SCHERR_CR_BAD_AT;
246 attrs1++;
249 return 0;
252 static int
253 cr_create_allowed(
254 ContentRule *scr,
255 int *op,
256 const char **err )
258 char **attrs = scr->scr_at_oids_may;
259 char **attrs1;
260 AttributeType *sat;
262 if ( attrs ) {
263 attrs1 = attrs;
264 while ( *attrs1 ) {
265 sat = at_find(*attrs1);
266 if ( !sat ) {
267 *err = *attrs1;
268 return SLAP_SCHERR_ATTR_NOT_FOUND;
271 if( is_at_operational( sat )) (*op)++;
273 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
274 at_find_in_list(sat, scr->scr_allowed) < 0 )
276 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
277 *err = *attrs1;
278 return SLAP_SCHERR_OUTOFMEM;
280 } else {
281 *err = *attrs1;
282 return SLAP_SCHERR_CR_BAD_AT;
284 attrs1++;
287 return 0;
290 static int
291 cr_create_precluded(
292 ContentRule *scr,
293 int *op,
294 const char **err )
296 char **attrs = scr->scr_at_oids_not;
297 char **attrs1;
298 AttributeType *sat;
300 if ( attrs ) {
301 attrs1 = attrs;
302 while ( *attrs1 ) {
303 sat = at_find(*attrs1);
304 if ( !sat ) {
305 *err = *attrs1;
306 return SLAP_SCHERR_ATTR_NOT_FOUND;
309 if( is_at_operational( sat )) (*op)++;
311 /* FIXME: should also make sure attribute type is not
312 a required attribute of the structural class or
313 any auxiliary class */
314 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
315 at_find_in_list(sat, scr->scr_allowed) < 0 &&
316 at_find_in_list(sat, scr->scr_precluded) < 0 )
318 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
319 *err = *attrs1;
320 return SLAP_SCHERR_OUTOFMEM;
322 } else {
323 *err = *attrs1;
324 return SLAP_SCHERR_CR_BAD_AT;
326 attrs1++;
329 return 0;
333 cr_add(
334 LDAPContentRule *cr,
335 int user,
336 ContentRule **rscr,
337 const char **err
340 ContentRule *scr;
341 int code;
342 int op = 0;
343 char *oidm = NULL;
345 if ( cr->cr_names != NULL ) {
346 int i;
348 for( i=0; cr->cr_names[i]; i++ ) {
349 if( !slap_valid_descr( cr->cr_names[i] ) ) {
350 return SLAP_SCHERR_BAD_DESCR;
355 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
356 /* Expand OID macros */
357 char *oid = oidm_find( cr->cr_oid );
358 if ( !oid ) {
359 *err = cr->cr_oid;
360 return SLAP_SCHERR_OIDM;
362 if ( oid != cr->cr_oid ) {
363 oidm = cr->cr_oid;
364 cr->cr_oid = oid;
368 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
369 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
371 scr->scr_oidmacro = oidm;
372 scr->scr_sclass = oc_find(cr->cr_oid);
373 if ( !scr->scr_sclass ) {
374 *err = cr->cr_oid;
375 code = SLAP_SCHERR_CLASS_NOT_FOUND;
376 goto fail;
379 /* check object class usage */
380 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
382 *err = cr->cr_oid;
383 code = SLAP_SCHERR_CR_BAD_STRUCT;
384 goto fail;
387 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
389 code = cr_add_auxiliaries( scr, &op, err );
390 if ( code != 0 ) goto fail;
392 code = cr_create_required( scr, &op, err );
393 if ( code != 0 ) goto fail;
395 code = cr_create_allowed( scr, &op, err );
396 if ( code != 0 ) goto fail;
398 code = cr_create_precluded( scr, &op, err );
399 if ( code != 0 ) goto fail;
401 if( user && op ) {
402 code = SLAP_SCHERR_CR_BAD_AUX;
403 goto fail;
406 code = cr_insert(scr,err);
407 if ( code == 0 && rscr )
408 *rscr = scr;
409 return code;
410 fail:
411 ch_free( scr );
412 return code;
415 void
416 cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
418 ContentRule *cr;
419 int i, num;
420 struct berval bv, *bva = NULL, idx;
421 char ibuf[32];
423 if ( !start )
424 start = LDAP_STAILQ_FIRST( &cr_list );
426 /* count the result size */
427 i = 0;
428 for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
429 if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
430 i++;
431 if ( cr == end ) break;
433 if (!i) return;
435 num = i;
436 bva = ch_malloc( (num+1) * sizeof(struct berval) );
437 BER_BVZERO( bva );
438 idx.bv_val = ibuf;
439 if ( sys ) {
440 idx.bv_len = 0;
441 ibuf[0] = '\0';
443 i = 0;
444 for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
445 LDAPContentRule lcr, *lcrp;
446 if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
447 if ( cr->scr_oidmacro ) {
448 lcr = cr->scr_crule;
449 lcr.cr_oid = cr->scr_oidmacro;
450 lcrp = &lcr;
451 } else {
452 lcrp = &cr->scr_crule;
454 if ( ldap_contentrule2bv( lcrp, &bv ) == NULL ) {
455 ber_bvarray_free( bva );
457 if ( !sys ) {
458 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
460 bva[i].bv_len = idx.bv_len + bv.bv_len;
461 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
462 strcpy( bva[i].bv_val, ibuf );
463 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
464 i++;
465 bva[i].bv_val = NULL;
466 ldap_memfree( bv.bv_val );
467 if ( cr == end ) break;
469 *res = bva;
473 cr_schema_info( Entry *e )
475 AttributeDescription *ad_ditContentRules
476 = slap_schema.si_ad_ditContentRules;
477 ContentRule *cr;
479 struct berval val;
480 struct berval nval;
482 LDAP_STAILQ_FOREACH(cr, &cr_list, scr_next) {
483 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
484 return -1;
487 #if 0
488 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
489 #endif
490 #if 0
491 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
492 (long) val.bv_len, val.bv_val, 0 );
493 #endif
495 nval.bv_val = cr->scr_oid;
496 nval.bv_len = strlen(cr->scr_oid);
498 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
500 return -1;
502 ldap_memfree( val.bv_val );
504 return 0;