1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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>
34 for( ; at != NULL; at = at->sat_sup ) {
35 if( at->sat_syntax_oid ) {
36 return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
47 for( ; sub != NULL; sub = sub->sat_sup ) {
48 if( sub == sup ) return 1;
55 struct berval air_name;
56 AttributeType *air_at;
59 static Avlnode *attr_index = NULL;
60 static Avlnode *attr_cache = NULL;
61 static LDAP_STAILQ_HEAD(ATList, slap_attribute_type) attr_list
62 = LDAP_STAILQ_HEAD_INITIALIZER(attr_list);
71 const struct aindexrec *air1 = v_air1;
72 const struct aindexrec *air2 = v_air2;
73 int i = air1->air_name.bv_len - air2->air_name.bv_len;
75 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
83 const struct berval *type = v_type;
84 const struct aindexrec *air = v_air;
85 int i = type->bv_len - air->air_name.bv_len;
87 return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
91 at_find( const char *name )
95 bv.bv_val = (char *)name;
96 bv.bv_len = strlen( name );
98 return at_bvfind( &bv );
102 at_bvfind( struct berval *name )
104 struct aindexrec *air;
107 air = avl_find( attr_cache, name, attr_index_name_cmp );
108 if ( air ) return air->air_at;
111 air = avl_find( attr_index, name, attr_index_name_cmp );
113 if ( air && ( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
114 avl_insert( &attr_cache, (caddr_t) air,
115 attr_index_cmp, avl_dup_error );
118 return air != NULL ? air->air_at : NULL;
124 AttributeType ***listp )
126 AttributeType **list;
127 AttributeType **list1;
133 list = ch_calloc(size, sizeof(AttributeType *));
145 list1 = ch_realloc(list, size*sizeof(AttributeType *));
160 AttributeType ***listp )
162 AttributeType **list;
163 AttributeType **list1;
171 for ( i=0; list[i]; i++ )
176 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
180 /* Tell the runtime this can be shrinked */
181 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
192 AttributeType **list )
199 for ( i=0; list[i]; i++ ) {
200 if ( sat == list[i] ) {
211 avl_free(attr_index, ldap_memfree);
213 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
214 a = LDAP_STAILQ_FIRST(&attr_list);
215 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
217 if ( a->sat_equality ) {
220 mr = mr_find( a->sat_equality->smr_oid );
221 assert( mr != NULL );
222 if ( mr != a->sat_equality ) {
223 ch_free( a->sat_equality );
224 a->sat_equality = NULL;
228 assert( a->sat_syntax != NULL );
229 if ( a->sat_syntax != NULL ) {
232 syn = syn_find( a->sat_syntax->ssyn_oid );
233 assert( syn != NULL );
234 if ( syn != a->sat_syntax ) {
235 ch_free( a->sat_syntax );
236 a->sat_syntax = NULL;
240 if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
241 if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
242 ad_destroy(a->sat_ad);
243 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
244 ldap_attributetype_free((LDAPAttributeType *)a);
247 if ( slap_schema.si_at_undefined ) {
248 ad_destroy(slap_schema.si_at_undefined->sat_ad);
251 if ( slap_schema.si_at_proxied ) {
252 ad_destroy(slap_schema.si_at_proxied->sat_ad);
257 at_start( AttributeType **at )
259 assert( at != NULL );
261 *at = LDAP_STAILQ_FIRST(&attr_list);
263 return (*at != NULL);
267 at_next( AttributeType **at )
269 assert( at != NULL );
271 #if 1 /* pedantic check */
273 AttributeType *tmp = NULL;
275 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
281 assert( tmp != NULL );
285 *at = LDAP_STAILQ_NEXT(*at,sat_next);
287 return (*at != NULL);
291 * check whether the two attributeTypes actually __are__ identical,
292 * or rather inconsistent
297 AttributeType *new_sat )
299 if ( new_sat->sat_oid != NULL ) {
300 if ( sat->sat_oid == NULL ) {
301 return SLAP_SCHERR_ATTR_INCONSISTENT;
304 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
305 return SLAP_SCHERR_ATTR_INCONSISTENT;
309 if ( sat->sat_oid != NULL ) {
310 return SLAP_SCHERR_ATTR_INCONSISTENT;
314 if ( new_sat->sat_names ) {
317 if ( sat->sat_names == NULL ) {
318 return SLAP_SCHERR_ATTR_INCONSISTENT;
321 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
322 if ( sat->sat_names[ i ] == NULL ) {
323 return SLAP_SCHERR_ATTR_INCONSISTENT;
326 if ( strcasecmp( sat->sat_names[ i ],
327 new_sat->sat_names[ i ] ) != 0 )
329 return SLAP_SCHERR_ATTR_INCONSISTENT;
333 if ( sat->sat_names != NULL ) {
334 return SLAP_SCHERR_ATTR_INCONSISTENT;
338 return SLAP_SCHERR_ATTR_DUP;
347 struct aindexrec *air;
351 if ( sat->sat_oid ) {
352 air = (struct aindexrec *)
353 ch_calloc( 1, sizeof(struct aindexrec) );
354 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
356 if ( avl_insert( &attr_index, (caddr_t) air,
357 attr_index_cmp, avl_dup_error ) )
359 AttributeType *old_sat;
364 old_sat = at_bvfind( &air->air_name );
365 assert( old_sat != NULL );
366 rc = at_check_dup( old_sat, sat );
372 /* FIX: temporal consistency check */
373 at_bvfind( &air->air_name );
376 names = sat->sat_names;
379 air = (struct aindexrec *)
380 ch_calloc( 1, sizeof(struct aindexrec) );
381 ber_str2bv( *names, 0, 0, &air->air_name );
383 if ( avl_insert( &attr_index, (caddr_t) air,
384 attr_index_cmp, avl_dup_error ) )
386 AttributeType *old_sat;
391 old_sat = at_bvfind( &air->air_name );
392 assert( old_sat != NULL );
393 rc = at_check_dup( old_sat, sat );
397 while ( names > sat->sat_names ) {
398 struct aindexrec tmpair;
401 ber_str2bv( *names, 0, 0, &tmpair.air_name );
403 air = (struct aindexrec *)avl_delete( &attr_index,
404 (caddr_t)&tmpair, attr_index_cmp );
405 assert( air != NULL );
409 if ( sat->sat_oid ) {
410 struct aindexrec tmpair;
412 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
414 air = (struct aindexrec *)avl_delete( &attr_index,
415 (caddr_t)&tmpair, attr_index_cmp );
416 assert( air != NULL );
422 /* FIX: temporal consistency check */
423 at_bvfind(&air->air_name);
428 if ( sat->sat_oid ) {
429 slap_ad_undef_promote( sat->sat_oid, sat );
432 names = sat->sat_names;
435 slap_ad_undef_promote( *names, sat );
440 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
447 LDAPAttributeType *at,
449 AttributeType **rsat,
452 AttributeType *sat = NULL;
453 MatchingRule *mr = NULL;
456 int code = LDAP_SUCCESS;
462 return SLAP_SCHERR_ATTR_INCOMPLETE;
465 if ( !OID_LEADCHAR( at->at_oid[0] )) {
468 /* Expand OID macros */
469 oid = oidm_find( at->at_oid );
472 return SLAP_SCHERR_OIDM;
474 if ( oid != at->at_oid ) {
480 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
483 /* Expand OID macros */
484 oid = oidm_find( at->at_syntax_oid );
486 *err = at->at_syntax_oid;
487 code = SLAP_SCHERR_OIDM;
490 if ( oid != at->at_syntax_oid ) {
491 ldap_memfree( at->at_syntax_oid );
492 at->at_syntax_oid = oid;
496 if ( at->at_names && at->at_names[0] ) {
499 for( i=0; at->at_names[i]; i++ ) {
500 if( !slap_valid_descr( at->at_names[i] ) ) {
501 *err = at->at_names[i];
502 code = SLAP_SCHERR_BAD_DESCR;
507 cname = at->at_names[0];
516 if ( !at->at_usage && at->at_no_user_mod ) {
517 /* user attribute must be modifable */
518 code = SLAP_SCHERR_ATTR_BAD_USAGE;
522 if ( at->at_collective ) {
524 /* collective attributes cannot be operational */
525 code = SLAP_SCHERR_ATTR_BAD_USAGE;
529 if( at->at_single_value ) {
530 /* collective attributes cannot be single-valued */
531 code = SLAP_SCHERR_ATTR_BAD_USAGE;
536 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
537 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
539 sat->sat_cname.bv_val = cname;
540 sat->sat_cname.bv_len = strlen( cname );
541 sat->sat_oidmacro = oidm;
542 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
544 if ( at->at_sup_oid ) {
545 AttributeType *supsat = at_find(at->at_sup_oid);
547 if ( supsat == NULL ) {
548 *err = at->at_sup_oid;
549 code = SLAP_SCHERR_ATTR_NOT_FOUND;
553 sat->sat_sup = supsat;
555 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
556 code = SLAP_SCHERR_OUTOFMEM;
560 if ( sat->sat_usage != supsat->sat_usage ) {
561 /* subtypes must have same usage as their SUP */
562 code = SLAP_SCHERR_ATTR_BAD_USAGE;
566 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
567 /* subtypes must be obsolete if super is */
568 code = SLAP_SCHERR_ATTR_BAD_SUP;
572 if ( sat->sat_flags & SLAP_AT_FINAL ) {
573 /* cannot subtype a "final" attribute type */
574 code = SLAP_SCHERR_ATTR_BAD_SUP;
580 * Inherit definitions from superiors. We only check the
581 * direct superior since that one has already inherited from
584 if ( sat->sat_sup ) {
585 sat->sat_syntax = sat->sat_sup->sat_syntax;
586 sat->sat_equality = sat->sat_sup->sat_equality;
587 sat->sat_approx = sat->sat_sup->sat_approx;
588 sat->sat_ordering = sat->sat_sup->sat_ordering;
589 sat->sat_substr = sat->sat_sup->sat_substr;
593 * check for X-ORDERED attributes
595 if ( sat->sat_extensions ) {
596 for (i=0; sat->sat_extensions[i]; i++) {
597 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
598 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
599 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
601 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
603 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
605 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
613 sat->sat_flags |= SLAP_AT_HARDCODE;
615 if ( at->at_syntax_oid ) {
616 syn = syn_find(sat->sat_syntax_oid);
618 *err = sat->sat_syntax_oid;
619 code = SLAP_SCHERR_SYN_NOT_FOUND;
623 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
624 code = SLAP_SCHERR_ATTR_BAD_SUP;
628 sat->sat_syntax = syn;
630 } else if ( sat->sat_syntax == NULL ) {
631 code = SLAP_SCHERR_ATTR_INCOMPLETE;
635 if ( sat->sat_equality_oid ) {
636 mr = mr_find(sat->sat_equality_oid);
639 *err = sat->sat_equality_oid;
640 code = SLAP_SCHERR_MR_NOT_FOUND;
644 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
645 *err = sat->sat_equality_oid;
646 code = SLAP_SCHERR_ATTR_BAD_MR;
650 if( sat->sat_syntax != mr->smr_syntax ) {
651 if( mr->smr_compat_syntaxes == NULL ) {
652 *err = sat->sat_equality_oid;
653 code = SLAP_SCHERR_ATTR_BAD_MR;
657 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
658 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
665 *err = sat->sat_equality_oid;
666 code = SLAP_SCHERR_ATTR_BAD_MR;
671 sat->sat_equality = mr;
672 sat->sat_approx = mr->smr_associated;
675 if ( sat->sat_ordering_oid ) {
676 if( !sat->sat_equality ) {
677 *err = sat->sat_ordering_oid;
678 code = SLAP_SCHERR_ATTR_BAD_MR;
682 mr = mr_find(sat->sat_ordering_oid);
685 *err = sat->sat_ordering_oid;
686 code = SLAP_SCHERR_MR_NOT_FOUND;
690 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
691 *err = sat->sat_ordering_oid;
692 code = SLAP_SCHERR_ATTR_BAD_MR;
696 if( sat->sat_syntax != mr->smr_syntax ) {
697 if( mr->smr_compat_syntaxes == NULL ) {
698 *err = sat->sat_ordering_oid;
699 code = SLAP_SCHERR_ATTR_BAD_MR;
703 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
704 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
711 *err = sat->sat_ordering_oid;
712 code = SLAP_SCHERR_ATTR_BAD_MR;
717 sat->sat_ordering = mr;
720 if ( sat->sat_substr_oid ) {
721 if( !sat->sat_equality ) {
722 *err = sat->sat_substr_oid;
723 code = SLAP_SCHERR_ATTR_BAD_MR;
727 mr = mr_find(sat->sat_substr_oid);
730 *err = sat->sat_substr_oid;
731 code = SLAP_SCHERR_MR_NOT_FOUND;
735 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
736 *err = sat->sat_substr_oid;
737 code = SLAP_SCHERR_ATTR_BAD_MR;
741 /* due to funky LDAP builtin substring rules,
742 * we check against the equality rule assertion
743 * syntax and compat syntaxes instead of those
744 * associated with the substrings rule.
746 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
747 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
748 *err = sat->sat_substr_oid;
749 code = SLAP_SCHERR_ATTR_BAD_MR;
753 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
754 if( sat->sat_syntax ==
755 sat->sat_equality->smr_compat_syntaxes[i] )
763 *err = sat->sat_substr_oid;
764 code = SLAP_SCHERR_ATTR_BAD_MR;
769 sat->sat_substr = mr;
772 code = at_insert( sat, err );
776 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
781 SLAP_FREE( at->at_oid );
795 at_index_printnode( void *v_air, void *ignore )
797 struct aindexrec *air = v_air;
799 air->air_name.bv_val,
800 ldap_attributetype2str(&air->air_at->sat_atype) );
805 at_index_print( void )
807 printf("Printing attribute type index:\n");
808 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
814 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
818 struct berval bv, *bva = NULL, idx;
822 start = LDAP_STAILQ_FIRST( &attr_list );
824 /* count the result size */
826 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
827 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
829 if ( at == end ) break;
834 bva = ch_malloc( (num+1) * sizeof(struct berval) );
842 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
843 LDAPAttributeType lat, *latp;
844 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
845 if ( at->sat_oidmacro ) {
847 lat.at_oid = at->sat_oidmacro;
850 latp = &at->sat_atype;
852 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
853 ber_bvarray_free( bva );
856 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
858 bva[i].bv_len = idx.bv_len + bv.bv_len;
859 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
860 strcpy( bva[i].bv_val, ibuf );
861 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
863 bva[i].bv_val = NULL;
864 ldap_memfree( bv.bv_val );
865 if ( at == end ) break;
871 at_schema_info( Entry *e )
873 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
878 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
879 if( at->sat_flags & SLAP_AT_HIDE ) continue;
881 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
885 ber_str2bv( at->sat_oid, 0, 0, &nval );
887 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
891 ldap_memfree( val.bv_val );