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);
64 /* Last hardcoded attribute registered */
65 AttributeType *at_sys_tail;
74 const struct aindexrec *air1 = v_air1;
75 const struct aindexrec *air2 = v_air2;
76 int i = air1->air_name.bv_len - air2->air_name.bv_len;
78 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
86 const struct berval *type = v_type;
87 const struct aindexrec *air = v_air;
88 int i = type->bv_len - air->air_name.bv_len;
90 return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
94 at_find( const char *name )
98 bv.bv_val = (char *)name;
99 bv.bv_len = strlen( name );
101 return at_bvfind( &bv );
105 at_bvfind( struct berval *name )
107 struct aindexrec *air;
110 air = avl_find( attr_cache, name, attr_index_name_cmp );
111 if ( air ) return air->air_at;
114 air = avl_find( attr_index, name, attr_index_name_cmp );
117 if ( air->air_at->sat_flags & SLAP_AT_DELETED ) {
119 } else if (( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
120 avl_insert( &attr_cache, (caddr_t) air,
121 attr_index_cmp, avl_dup_error );
125 return air != NULL ? air->air_at : NULL;
131 AttributeType ***listp )
133 AttributeType **list;
134 AttributeType **list1;
140 list = ch_calloc(size, sizeof(AttributeType *));
152 list1 = ch_realloc(list, size*sizeof(AttributeType *));
167 AttributeType ***listp )
169 AttributeType **list;
170 AttributeType **list1;
178 for ( i=0; list[i]; i++ )
183 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
187 /* Tell the runtime this can be shrinked */
188 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
199 AttributeType **list )
206 for ( i=0; list[i]; i++ ) {
207 if ( sat == list[i] ) {
215 at_delete_names( AttributeType *at )
217 char **names = at->sat_names;
220 struct aindexrec tmpair, *air;
222 ber_str2bv( *names, 0, 0, &tmpair.air_name );
224 air = (struct aindexrec *)avl_delete( &attr_index,
225 (caddr_t)&tmpair, attr_index_cmp );
226 assert( air != NULL );
232 /* Mark the attribute as deleted, remove from list, and remove all its
233 * names from the AVL tree. Leave the OID in the tree.
236 at_delete( AttributeType *at )
238 at->sat_flags |= SLAP_AT_DELETED;
240 LDAP_STAILQ_REMOVE(&attr_list,at,slap_attribute_type,sat_next);
242 at_delete_names( at );
246 at_clean( AttributeType *a )
248 if ( a->sat_equality ) {
251 mr = mr_find( a->sat_equality->smr_oid );
252 assert( mr != NULL );
253 if ( mr != a->sat_equality ) {
254 ch_free( a->sat_equality );
255 a->sat_equality = NULL;
259 assert( a->sat_syntax != NULL );
260 if ( a->sat_syntax != NULL ) {
263 syn = syn_find( a->sat_syntax->ssyn_oid );
264 assert( syn != NULL );
265 if ( syn != a->sat_syntax ) {
266 ch_free( a->sat_syntax );
267 a->sat_syntax = NULL;
271 if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
272 if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
276 at_destroy_one( void *v )
278 struct aindexrec *air = v;
279 AttributeType *a = air->air_at;
282 ad_destroy(a->sat_ad);
283 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
284 ldap_attributetype_free((LDAPAttributeType *)a);
293 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
294 a = LDAP_STAILQ_FIRST(&attr_list);
295 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
297 at_delete_names( a );
300 avl_free(attr_index, at_destroy_one);
302 if ( slap_schema.si_at_undefined ) {
303 ad_destroy(slap_schema.si_at_undefined->sat_ad);
306 if ( slap_schema.si_at_proxied ) {
307 ad_destroy(slap_schema.si_at_proxied->sat_ad);
312 at_start( AttributeType **at )
314 assert( at != NULL );
316 *at = LDAP_STAILQ_FIRST(&attr_list);
318 return (*at != NULL);
322 at_next( AttributeType **at )
324 assert( at != NULL );
326 #if 1 /* pedantic check */
328 AttributeType *tmp = NULL;
330 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
336 assert( tmp != NULL );
340 *at = LDAP_STAILQ_NEXT(*at,sat_next);
342 return (*at != NULL);
346 * check whether the two attributeTypes actually __are__ identical,
347 * or rather inconsistent
352 AttributeType *new_sat )
354 if ( new_sat->sat_oid != NULL ) {
355 if ( sat->sat_oid == NULL ) {
356 return SLAP_SCHERR_ATTR_INCONSISTENT;
359 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
360 return SLAP_SCHERR_ATTR_INCONSISTENT;
364 if ( sat->sat_oid != NULL ) {
365 return SLAP_SCHERR_ATTR_INCONSISTENT;
369 if ( new_sat->sat_names ) {
372 if ( sat->sat_names == NULL ) {
373 return SLAP_SCHERR_ATTR_INCONSISTENT;
376 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
377 if ( sat->sat_names[ i ] == NULL ) {
378 return SLAP_SCHERR_ATTR_INCONSISTENT;
381 if ( strcasecmp( sat->sat_names[ i ],
382 new_sat->sat_names[ i ] ) != 0 )
384 return SLAP_SCHERR_ATTR_INCONSISTENT;
388 if ( sat->sat_names != NULL ) {
389 return SLAP_SCHERR_ATTR_INCONSISTENT;
393 return SLAP_SCHERR_ATTR_DUP;
396 static struct aindexrec *air_old;
399 at_dup_error( void *left, void *right )
411 struct aindexrec *air;
413 AttributeType *sat = *rat;
415 if ( sat->sat_oid ) {
416 air = (struct aindexrec *)
417 ch_calloc( 1, sizeof(struct aindexrec) );
418 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
422 if ( avl_insert( &attr_index, (caddr_t) air,
423 attr_index_cmp, at_dup_error ) )
425 AttributeType *old_sat;
430 assert( air_old != NULL );
431 old_sat = air_old->air_at;
433 /* replacing a deleted definition? */
434 if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
437 /* Keep old oid, free new oid;
438 * Keep old ads, free new ads;
439 * Keep new everything else, free old
443 old_sat->sat_oid = tmp.sat_oid;
444 tmp.sat_oid = sat->sat_oid;
445 old_sat->sat_ad = tmp.sat_ad;
446 tmp.sat_ad = sat->sat_ad;
450 at_destroy_one( air );
458 rc = at_check_dup( old_sat, sat );
463 /* FIX: temporal consistency check */
464 at_bvfind( &air->air_name );
467 names = sat->sat_names;
470 air = (struct aindexrec *)
471 ch_calloc( 1, sizeof(struct aindexrec) );
472 ber_str2bv( *names, 0, 0, &air->air_name );
474 if ( avl_insert( &attr_index, (caddr_t) air,
475 attr_index_cmp, avl_dup_error ) )
477 AttributeType *old_sat;
482 old_sat = at_bvfind( &air->air_name );
483 assert( old_sat != NULL );
484 rc = at_check_dup( old_sat, sat );
488 while ( names > sat->sat_names ) {
489 struct aindexrec tmpair;
492 ber_str2bv( *names, 0, 0, &tmpair.air_name );
494 air = (struct aindexrec *)avl_delete( &attr_index,
495 (caddr_t)&tmpair, attr_index_cmp );
496 assert( air != NULL );
500 if ( sat->sat_oid ) {
501 struct aindexrec tmpair;
503 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
505 air = (struct aindexrec *)avl_delete( &attr_index,
506 (caddr_t)&tmpair, attr_index_cmp );
507 assert( air != NULL );
513 /* FIX: temporal consistency check */
514 at_bvfind(&air->air_name);
519 if ( sat->sat_oid ) {
520 slap_ad_undef_promote( sat->sat_oid, sat );
523 names = sat->sat_names;
526 slap_ad_undef_promote( *names, sat );
531 if ( sat->sat_flags & SLAP_AT_HARDCODE ) {
536 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
538 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
546 LDAPAttributeType *at,
548 AttributeType **rsat,
552 AttributeType *sat = NULL;
553 MatchingRule *mr = NULL;
556 int code = LDAP_SUCCESS;
562 return SLAP_SCHERR_ATTR_INCOMPLETE;
565 if ( !OID_LEADCHAR( at->at_oid[0] )) {
568 /* Expand OID macros */
569 oid = oidm_find( at->at_oid );
572 return SLAP_SCHERR_OIDM;
574 if ( oid != at->at_oid ) {
580 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
583 /* Expand OID macros */
584 oid = oidm_find( at->at_syntax_oid );
586 *err = at->at_syntax_oid;
587 code = SLAP_SCHERR_OIDM;
590 if ( oid != at->at_syntax_oid ) {
591 ldap_memfree( at->at_syntax_oid );
592 at->at_syntax_oid = oid;
596 if ( at->at_names && at->at_names[0] ) {
599 for( i=0; at->at_names[i]; i++ ) {
600 if( !slap_valid_descr( at->at_names[i] ) ) {
601 *err = at->at_names[i];
602 code = SLAP_SCHERR_BAD_DESCR;
607 cname = at->at_names[0];
616 if ( !at->at_usage && at->at_no_user_mod ) {
617 /* user attribute must be modifable */
618 code = SLAP_SCHERR_ATTR_BAD_USAGE;
622 if ( at->at_collective ) {
624 /* collective attributes cannot be operational */
625 code = SLAP_SCHERR_ATTR_BAD_USAGE;
629 if( at->at_single_value ) {
630 /* collective attributes cannot be single-valued */
631 code = SLAP_SCHERR_ATTR_BAD_USAGE;
636 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
637 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
639 sat->sat_cname.bv_val = cname;
640 sat->sat_cname.bv_len = strlen( cname );
641 sat->sat_oidmacro = oidm;
642 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
644 if ( at->at_sup_oid ) {
645 AttributeType *supsat = at_find(at->at_sup_oid);
647 if ( supsat == NULL ) {
648 *err = at->at_sup_oid;
649 code = SLAP_SCHERR_ATTR_NOT_FOUND;
653 sat->sat_sup = supsat;
655 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
656 code = SLAP_SCHERR_OUTOFMEM;
660 if ( sat->sat_usage != supsat->sat_usage ) {
661 /* subtypes must have same usage as their SUP */
662 code = SLAP_SCHERR_ATTR_BAD_USAGE;
666 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
667 /* subtypes must be obsolete if super is */
668 code = SLAP_SCHERR_ATTR_BAD_SUP;
672 if ( sat->sat_flags & SLAP_AT_FINAL ) {
673 /* cannot subtype a "final" attribute type */
674 code = SLAP_SCHERR_ATTR_BAD_SUP;
680 * Inherit definitions from superiors. We only check the
681 * direct superior since that one has already inherited from
684 if ( sat->sat_sup ) {
685 sat->sat_syntax = sat->sat_sup->sat_syntax;
686 sat->sat_equality = sat->sat_sup->sat_equality;
687 sat->sat_approx = sat->sat_sup->sat_approx;
688 sat->sat_ordering = sat->sat_sup->sat_ordering;
689 sat->sat_substr = sat->sat_sup->sat_substr;
693 * check for X-ORDERED attributes
695 if ( sat->sat_extensions ) {
696 for (i=0; sat->sat_extensions[i]; i++) {
697 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
698 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
699 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
701 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
703 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
705 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
713 sat->sat_flags |= SLAP_AT_HARDCODE;
715 if ( at->at_syntax_oid ) {
716 syn = syn_find(sat->sat_syntax_oid);
718 *err = sat->sat_syntax_oid;
719 code = SLAP_SCHERR_SYN_NOT_FOUND;
723 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
724 code = SLAP_SCHERR_ATTR_BAD_SUP;
728 sat->sat_syntax = syn;
730 } else if ( sat->sat_syntax == NULL ) {
731 code = SLAP_SCHERR_ATTR_INCOMPLETE;
735 if ( sat->sat_equality_oid ) {
736 mr = mr_find(sat->sat_equality_oid);
739 *err = sat->sat_equality_oid;
740 code = SLAP_SCHERR_MR_NOT_FOUND;
744 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
745 *err = sat->sat_equality_oid;
746 code = SLAP_SCHERR_ATTR_BAD_MR;
750 if( sat->sat_syntax != mr->smr_syntax ) {
751 if( mr->smr_compat_syntaxes == NULL ) {
752 *err = sat->sat_equality_oid;
753 code = SLAP_SCHERR_ATTR_BAD_MR;
757 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
758 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
765 *err = sat->sat_equality_oid;
766 code = SLAP_SCHERR_ATTR_BAD_MR;
771 sat->sat_equality = mr;
772 sat->sat_approx = mr->smr_associated;
775 if ( sat->sat_ordering_oid ) {
776 if( !sat->sat_equality ) {
777 *err = sat->sat_ordering_oid;
778 code = SLAP_SCHERR_ATTR_BAD_MR;
782 mr = mr_find(sat->sat_ordering_oid);
785 *err = sat->sat_ordering_oid;
786 code = SLAP_SCHERR_MR_NOT_FOUND;
790 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
791 *err = sat->sat_ordering_oid;
792 code = SLAP_SCHERR_ATTR_BAD_MR;
796 if( sat->sat_syntax != mr->smr_syntax ) {
797 if( mr->smr_compat_syntaxes == NULL ) {
798 *err = sat->sat_ordering_oid;
799 code = SLAP_SCHERR_ATTR_BAD_MR;
803 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
804 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
811 *err = sat->sat_ordering_oid;
812 code = SLAP_SCHERR_ATTR_BAD_MR;
817 sat->sat_ordering = mr;
820 if ( sat->sat_substr_oid ) {
821 if( !sat->sat_equality ) {
822 *err = sat->sat_substr_oid;
823 code = SLAP_SCHERR_ATTR_BAD_MR;
827 mr = mr_find(sat->sat_substr_oid);
830 *err = sat->sat_substr_oid;
831 code = SLAP_SCHERR_MR_NOT_FOUND;
835 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
836 *err = sat->sat_substr_oid;
837 code = SLAP_SCHERR_ATTR_BAD_MR;
841 /* due to funky LDAP builtin substring rules,
842 * we check against the equality rule assertion
843 * syntax and compat syntaxes instead of those
844 * associated with the substrings rule.
846 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
847 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
848 *err = sat->sat_substr_oid;
849 code = SLAP_SCHERR_ATTR_BAD_MR;
853 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
854 if( sat->sat_syntax ==
855 sat->sat_equality->smr_compat_syntaxes[i] )
863 *err = sat->sat_substr_oid;
864 code = SLAP_SCHERR_ATTR_BAD_MR;
869 sat->sat_substr = mr;
872 code = at_insert( &sat, prev, err );
876 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
881 SLAP_FREE( at->at_oid );
895 at_index_printnode( void *v_air, void *ignore )
897 struct aindexrec *air = v_air;
899 air->air_name.bv_val,
900 ldap_attributetype2str(&air->air_at->sat_atype) );
905 at_index_print( void )
907 printf("Printing attribute type index:\n");
908 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
914 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
918 struct berval bv, *bva = NULL, idx;
922 start = LDAP_STAILQ_FIRST( &attr_list );
924 /* count the result size */
926 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
927 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
929 if ( at == end ) break;
934 bva = ch_malloc( (num+1) * sizeof(struct berval) );
942 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
943 LDAPAttributeType lat, *latp;
944 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
945 if ( at->sat_oidmacro ) {
947 lat.at_oid = at->sat_oidmacro;
950 latp = &at->sat_atype;
952 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
953 ber_bvarray_free( bva );
956 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
958 bva[i].bv_len = idx.bv_len + bv.bv_len;
959 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
960 strcpy( bva[i].bv_val, ibuf );
961 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
963 bva[i].bv_val = NULL;
964 ldap_memfree( bv.bv_val );
965 if ( at == end ) break;
971 at_schema_info( Entry *e )
973 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
978 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
979 if( at->sat_flags & SLAP_AT_HIDE ) continue;
981 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
985 ber_str2bv( at->sat_oid, 0, 0, &nval );
987 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
991 ldap_memfree( val.bv_val );
997 register_at( char *def, AttributeDescription **rad, int dupok )
999 LDAPAttributeType *at;
1000 int code, freeit = 0;
1002 AttributeDescription *ad = NULL;
1004 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1006 Debug( LDAP_DEBUG_ANY,
1007 "register_at: AttributeType \"%s\": %s, %s\n",
1008 def, ldap_scherr2str(code), err );
1012 code = at_add( at, 0, NULL, NULL, &err );
1014 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1018 ldap_attributetype_free( at );
1019 Debug( LDAP_DEBUG_ANY,
1020 "register_at: AttributeType \"%s\": %s, %s\n",
1021 def, scherr2str(code), err );
1025 code = slap_str2ad( at->at_names[0], &ad, &err );
1026 if ( freeit || code ) {
1027 ldap_attributetype_free( at );
1032 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1035 if ( rad ) *rad = ad;