1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
23 #include <ac/socket.h>
24 #include <ac/string.h>
35 for( ; at != NULL; at = at->sat_sup ) {
36 if( at->sat_syntax_oid ) {
37 return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
48 for( ; sub != NULL; sub = sub->sat_sup ) {
49 if( sub == sup ) return 1;
56 struct berval air_name;
57 AttributeType *air_at;
60 static Avlnode *attr_index = NULL;
61 static LDAP_SLIST_HEAD(ATList, slap_attribute_type) attr_list
62 = LDAP_SLIST_HEAD_INITIALIZER(&attr_list);
69 const struct aindexrec *air1 = v_air1;
70 const struct aindexrec *air2 = v_air2;
71 int i = air1->air_name.bv_len - air2->air_name.bv_len;
73 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
81 const struct berval *type = v_type;
82 const struct aindexrec *air = v_air;
83 int i = type->bv_len - air->air_name.bv_len;
85 return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
89 at_find( const char *name )
93 bv.bv_val = (char *)name;
94 bv.bv_len = strlen( name );
96 return at_bvfind( &bv );
100 at_bvfind( struct berval *name )
102 struct aindexrec *air;
104 air = avl_find( attr_index, name, attr_index_name_cmp );
106 return air != NULL ? air->air_at : NULL;
112 AttributeType ***listp )
114 AttributeType **list;
115 AttributeType **list1;
121 list = ch_calloc(size, sizeof(AttributeType *));
133 list1 = ch_realloc(list, size*sizeof(AttributeType *));
148 AttributeType ***listp )
150 AttributeType **list;
151 AttributeType **list1;
159 for ( i=0; list[i]; i++ )
164 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
168 /* Tell the runtime this can be shrinked */
169 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
180 AttributeType **list )
187 for ( i=0; list[i]; i++ ) {
188 if ( sat == list[i] ) {
199 avl_free(attr_index, ldap_memfree);
201 while( !LDAP_SLIST_EMPTY(&attr_list) ) {
202 a = LDAP_SLIST_FIRST(&attr_list);
203 LDAP_SLIST_REMOVE_HEAD(&attr_list, sat_next);
205 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
206 ad_destroy(a->sat_ad);
207 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
208 ldap_attributetype_free((LDAPAttributeType *)a);
211 if ( slap_schema.si_at_undefined ) {
212 ad_destroy(slap_schema.si_at_undefined->sat_ad);
217 at_start( AttributeType **at )
221 *at = LDAP_SLIST_FIRST(&attr_list);
223 return (*at != NULL);
227 at_next( AttributeType **at )
231 #if 1 /* pedantic check */
233 AttributeType *tmp = NULL;
235 LDAP_SLIST_FOREACH(tmp,&attr_list,sat_next) {
245 *at = LDAP_SLIST_NEXT(*at,sat_next);
247 return (*at != NULL);
257 struct aindexrec *air;
260 LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
261 LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
263 if ( sat->sat_oid ) {
264 air = (struct aindexrec *)
265 ch_calloc( 1, sizeof(struct aindexrec) );
266 air->air_name.bv_val = sat->sat_oid;
267 air->air_name.bv_len = strlen(sat->sat_oid);
269 if ( avl_insert( &attr_index, (caddr_t) air,
270 attr_index_cmp, avl_dup_error ) ) {
273 return SLAP_SCHERR_ATTR_DUP;
275 /* FIX: temporal consistency check */
276 at_bvfind(&air->air_name);
279 if ( (names = sat->sat_names) ) {
281 air = (struct aindexrec *)
282 ch_calloc( 1, sizeof(struct aindexrec) );
283 air->air_name.bv_val = *names;
284 air->air_name.bv_len = strlen(*names);
286 if ( avl_insert( &attr_index, (caddr_t) air,
287 attr_index_cmp, avl_dup_error ) ) {
290 return SLAP_SCHERR_ATTR_DUP;
292 /* FIX: temporal consistency check */
293 at_bvfind(&air->air_name);
303 LDAPAttributeType *at,
314 if ( !OID_LEADCHAR( at->at_oid[0] )) {
315 /* Expand OID macros */
316 oid = oidm_find( at->at_oid );
319 return SLAP_SCHERR_OIDM;
321 if ( oid != at->at_oid ) {
322 ldap_memfree( at->at_oid );
327 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
328 /* Expand OID macros */
329 oid = oidm_find( at->at_syntax_oid );
331 *err = at->at_syntax_oid;
332 return SLAP_SCHERR_OIDM;
334 if ( oid != at->at_syntax_oid ) {
335 ldap_memfree( at->at_syntax_oid );
336 at->at_syntax_oid = oid;
340 if ( at->at_names && at->at_names[0] ) {
343 for( i=0; at->at_names[i]; i++ ) {
344 if( !slap_valid_descr( at->at_names[i] ) ) {
345 *err = at->at_names[i];
346 return SLAP_SCHERR_BAD_DESCR;
350 cname = at->at_names[0];
352 } else if ( at->at_oid ) {
357 return SLAP_SCHERR_ATTR_INCOMPLETE;
362 if ( !at->at_usage && at->at_no_user_mod ) {
363 /* user attribute must be modifable */
364 return SLAP_SCHERR_ATTR_BAD_USAGE;
367 if ( at->at_collective ) {
369 /* collective attributes cannot be operational */
370 return SLAP_SCHERR_ATTR_BAD_USAGE;
373 if( at->at_single_value ) {
374 /* collective attributes cannot be single-valued */
375 return SLAP_SCHERR_ATTR_BAD_USAGE;
379 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
380 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
382 sat->sat_cname.bv_val = cname;
383 sat->sat_cname.bv_len = strlen( cname );
384 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
386 if ( at->at_sup_oid ) {
387 AttributeType *supsat = at_find(at->at_sup_oid);
389 if ( supsat == NULL ) {
390 *err = at->at_sup_oid;
391 return SLAP_SCHERR_ATTR_NOT_FOUND;
394 sat->sat_sup = supsat;
396 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
397 return SLAP_SCHERR_OUTOFMEM;
400 if ( sat->sat_usage != supsat->sat_usage ) {
401 /* subtypes must have same usage as their SUP */
402 return SLAP_SCHERR_ATTR_BAD_USAGE;
405 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
406 /* subtypes must be obsolete if super is */
407 return SLAP_SCHERR_ATTR_BAD_SUP;
410 if ( sat->sat_flags & SLAP_AT_FINAL ) {
411 /* cannot subtype a "final" attribute type */
412 return SLAP_SCHERR_ATTR_BAD_SUP;
417 * Inherit definitions from superiors. We only check the
418 * direct superior since that one has already inherited from
421 if ( sat->sat_sup ) {
422 sat->sat_syntax = sat->sat_sup->sat_syntax;
423 sat->sat_equality = sat->sat_sup->sat_equality;
424 sat->sat_approx = sat->sat_sup->sat_approx;
425 sat->sat_ordering = sat->sat_sup->sat_ordering;
426 sat->sat_substr = sat->sat_sup->sat_substr;
429 if ( at->at_syntax_oid ) {
430 syn = syn_find(sat->sat_syntax_oid);
432 *err = sat->sat_syntax_oid;
433 return SLAP_SCHERR_SYN_NOT_FOUND;
436 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
437 return SLAP_SCHERR_ATTR_BAD_SUP;
440 sat->sat_syntax = syn;
442 } else if ( sat->sat_syntax == NULL ) {
443 return SLAP_SCHERR_ATTR_INCOMPLETE;
446 if ( sat->sat_equality_oid ) {
447 mr = mr_find(sat->sat_equality_oid);
450 *err = sat->sat_equality_oid;
451 return SLAP_SCHERR_MR_NOT_FOUND;
454 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
455 *err = sat->sat_equality_oid;
456 return SLAP_SCHERR_ATTR_BAD_MR;
459 if( sat->sat_syntax != mr->smr_syntax ) {
460 if( mr->smr_compat_syntaxes == NULL ) {
461 *err = sat->sat_equality_oid;
462 return SLAP_SCHERR_ATTR_BAD_MR;
465 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
466 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
473 *err = sat->sat_equality_oid;
474 return SLAP_SCHERR_ATTR_BAD_MR;
478 sat->sat_equality = mr;
479 sat->sat_approx = mr->smr_associated;
482 if ( sat->sat_ordering_oid ) {
483 if( !sat->sat_equality ) {
484 *err = sat->sat_ordering_oid;
485 return SLAP_SCHERR_ATTR_BAD_MR;
488 mr = mr_find(sat->sat_ordering_oid);
491 *err = sat->sat_ordering_oid;
492 return SLAP_SCHERR_MR_NOT_FOUND;
495 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
496 *err = sat->sat_ordering_oid;
497 return SLAP_SCHERR_ATTR_BAD_MR;
500 if( sat->sat_syntax != mr->smr_syntax ) {
501 if( mr->smr_compat_syntaxes == NULL ) {
502 *err = sat->sat_ordering_oid;
503 return SLAP_SCHERR_ATTR_BAD_MR;
506 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
507 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
514 *err = sat->sat_ordering_oid;
515 return SLAP_SCHERR_ATTR_BAD_MR;
519 sat->sat_ordering = mr;
522 if ( sat->sat_substr_oid ) {
523 if( !sat->sat_equality ) {
524 *err = sat->sat_substr_oid;
525 return SLAP_SCHERR_ATTR_BAD_MR;
528 mr = mr_find(sat->sat_substr_oid);
531 *err = sat->sat_substr_oid;
532 return SLAP_SCHERR_MR_NOT_FOUND;
535 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
536 *err = sat->sat_substr_oid;
537 return SLAP_SCHERR_ATTR_BAD_MR;
540 /* due to funky LDAP builtin substring rules,
541 * we check against the equality rule assertion
542 * syntax and compat syntaxes instead of those
543 * associated with the substrings rule.
545 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
546 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
547 *err = sat->sat_substr_oid;
548 return SLAP_SCHERR_ATTR_BAD_MR;
551 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
552 if( sat->sat_syntax ==
553 sat->sat_equality->smr_compat_syntaxes[i] )
561 *err = sat->sat_substr_oid;
562 return SLAP_SCHERR_ATTR_BAD_MR;
566 sat->sat_substr = mr;
569 code = at_insert(sat,err);
575 at_index_printnode( void *v_air, void *ignore )
577 struct aindexrec *air = v_air;
579 air->air_name.bv_val,
580 ldap_attributetype2str(&air->air_at->sat_atype) );
585 at_index_print( void )
587 printf("Printing attribute type index:\n");
588 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
593 at_schema_info( Entry *e )
595 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
600 LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
601 if( at->sat_flags & SLAP_AT_HIDE ) continue;
603 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
607 nval.bv_val = at->sat_oid;
608 nval.bv_len = strlen(at->sat_oid);
610 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
614 ldap_memfree( val.bv_val );