1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2015 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 at->sat_syntax_oid;
50 const char *syn_oid = at_syntax( at );
53 return strcmp( syn_oid, oid ) == 0;
63 for( ; sub != NULL; sub = sub->sat_sup ) {
64 if( sub == sup ) return 1;
71 struct berval air_name;
72 AttributeType *air_at;
75 static Avlnode *attr_index = NULL;
76 static Avlnode *attr_cache = NULL;
77 static LDAP_STAILQ_HEAD(ATList, AttributeType) attr_list
78 = LDAP_STAILQ_HEAD_INITIALIZER(attr_list);
80 /* Last hardcoded attribute registered */
81 AttributeType *at_sys_tail;
90 const struct aindexrec *air1 = v_air1;
91 const struct aindexrec *air2 = v_air2;
92 int i = air1->air_name.bv_len - air2->air_name.bv_len;
94 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
102 const struct berval *type = v_type;
103 const struct aindexrec *air = v_air;
104 int i = type->bv_len - air->air_name.bv_len;
106 return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
110 at_find( const char *name )
114 bv.bv_val = (char *)name;
115 bv.bv_len = strlen( name );
117 return at_bvfind( &bv );
121 at_bvfind( struct berval *name )
123 struct aindexrec *air;
126 air = avl_find( attr_cache, name, attr_index_name_cmp );
127 if ( air ) return air->air_at;
130 air = avl_find( attr_index, name, attr_index_name_cmp );
133 if ( air->air_at->sat_flags & SLAP_AT_DELETED ) {
135 } else if (( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
136 avl_insert( &attr_cache, (caddr_t) air,
137 attr_index_cmp, avl_dup_error );
141 return air != NULL ? air->air_at : NULL;
147 AttributeType ***listp )
149 AttributeType **list;
150 AttributeType **list1;
156 list = ch_calloc(size, sizeof(AttributeType *));
168 list1 = ch_realloc(list, size*sizeof(AttributeType *));
183 AttributeType ***listp )
185 AttributeType **list;
186 AttributeType **list1;
194 for ( i=0; list[i]; i++ )
199 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
203 /* Tell the runtime this can be shrinked */
204 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
215 AttributeType **list )
222 for ( i=0; list[i]; i++ ) {
223 if ( sat == list[i] ) {
231 at_delete_names( AttributeType *at )
233 char **names = at->sat_names;
238 struct aindexrec tmpair, *air;
240 ber_str2bv( *names, 0, 0, &tmpair.air_name );
242 air = (struct aindexrec *)avl_delete( &attr_index,
243 (caddr_t)&tmpair, attr_index_cmp );
244 assert( air != NULL );
250 /* Mark the attribute as deleted, remove from list, and remove all its
251 * names from the AVL tree. Leave the OID in the tree.
254 at_delete( AttributeType *at )
256 at->sat_flags |= SLAP_AT_DELETED;
258 LDAP_STAILQ_REMOVE(&attr_list, at, AttributeType, sat_next);
260 at_delete_names( at );
264 at_clean( AttributeType *a )
266 if ( a->sat_equality ) {
269 mr = mr_find( a->sat_equality->smr_oid );
270 assert( mr != NULL );
271 if ( mr != a->sat_equality ) {
272 ch_free( a->sat_equality );
273 a->sat_equality = NULL;
277 assert( a->sat_syntax != NULL );
278 if ( a->sat_syntax != NULL ) {
281 syn = syn_find( a->sat_syntax->ssyn_oid );
282 assert( syn != NULL );
283 if ( syn != a->sat_syntax ) {
284 ch_free( a->sat_syntax );
285 a->sat_syntax = NULL;
289 if ( a->sat_oidmacro ) {
290 ldap_memfree( a->sat_oidmacro );
291 a->sat_oidmacro = NULL;
293 if ( a->sat_soidmacro ) {
294 ldap_memfree( a->sat_soidmacro );
295 a->sat_soidmacro = NULL;
297 if ( a->sat_subtypes ) {
298 ldap_memfree( a->sat_subtypes );
299 a->sat_subtypes = NULL;
304 at_destroy_one( void *v )
306 struct aindexrec *air = v;
307 AttributeType *a = air->air_at;
310 ad_destroy(a->sat_ad);
311 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
312 ldap_attributetype_free((LDAPAttributeType *)a);
321 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
322 a = LDAP_STAILQ_FIRST(&attr_list);
323 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
325 at_delete_names( a );
328 avl_free(attr_index, at_destroy_one);
330 if ( slap_schema.si_at_undefined ) {
331 ad_destroy(slap_schema.si_at_undefined->sat_ad);
334 if ( slap_schema.si_at_proxied ) {
335 ad_destroy(slap_schema.si_at_proxied->sat_ad);
340 at_start( AttributeType **at )
342 assert( at != NULL );
344 *at = LDAP_STAILQ_FIRST(&attr_list);
346 return (*at != NULL);
350 at_next( AttributeType **at )
352 assert( at != NULL );
354 #if 0 /* pedantic check: don't use this */
356 AttributeType *tmp = NULL;
358 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
364 assert( tmp != NULL );
368 *at = LDAP_STAILQ_NEXT(*at,sat_next);
370 return (*at != NULL);
374 * check whether the two attributeTypes actually __are__ identical,
375 * or rather inconsistent
380 AttributeType *new_sat )
382 if ( new_sat->sat_oid != NULL ) {
383 if ( sat->sat_oid == NULL ) {
384 return SLAP_SCHERR_ATTR_INCONSISTENT;
387 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
388 return SLAP_SCHERR_ATTR_INCONSISTENT;
392 if ( sat->sat_oid != NULL ) {
393 return SLAP_SCHERR_ATTR_INCONSISTENT;
397 if ( new_sat->sat_names ) {
400 if ( sat->sat_names == NULL ) {
401 return SLAP_SCHERR_ATTR_INCONSISTENT;
404 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
405 if ( sat->sat_names[ i ] == NULL ) {
406 return SLAP_SCHERR_ATTR_INCONSISTENT;
409 if ( strcasecmp( sat->sat_names[ i ],
410 new_sat->sat_names[ i ] ) != 0 )
412 return SLAP_SCHERR_ATTR_INCONSISTENT;
416 if ( sat->sat_names != NULL ) {
417 return SLAP_SCHERR_ATTR_INCONSISTENT;
421 return SLAP_SCHERR_ATTR_DUP;
424 static struct aindexrec *air_old;
427 at_dup_error( void *left, void *right )
439 struct aindexrec *air;
441 AttributeType *sat = *rat;
443 if ( sat->sat_oid ) {
444 air = (struct aindexrec *)
445 ch_calloc( 1, sizeof(struct aindexrec) );
446 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
450 if ( avl_insert( &attr_index, (caddr_t) air,
451 attr_index_cmp, at_dup_error ) )
453 AttributeType *old_sat;
458 assert( air_old != NULL );
459 old_sat = air_old->air_at;
461 /* replacing a deleted definition? */
462 if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
464 AttributeDescription *ad;
466 /* Keep old oid, free new oid;
467 * Keep old ads, free new ads;
468 * Keep old ad_mutex, free new ad_mutex;
469 * Keep new everything else, free old
473 old_sat->sat_oid = tmp.sat_oid;
474 tmp.sat_oid = sat->sat_oid;
475 old_sat->sat_ad = tmp.sat_ad;
476 tmp.sat_ad = sat->sat_ad;
477 old_sat->sat_ad_mutex = tmp.sat_ad_mutex;
478 tmp.sat_ad_mutex = sat->sat_ad_mutex;
481 /* Check for basic ad pointing at old cname */
482 for ( ad = old_sat->sat_ad; ad; ad=ad->ad_next ) {
483 if ( ad->ad_cname.bv_val == sat->sat_cname.bv_val ) {
484 ad->ad_cname = old_sat->sat_cname;
490 at_destroy_one( air );
498 rc = at_check_dup( old_sat, sat );
503 /* FIX: temporal consistency check */
504 at_bvfind( &air->air_name );
507 names = sat->sat_names;
510 air = (struct aindexrec *)
511 ch_calloc( 1, sizeof(struct aindexrec) );
512 ber_str2bv( *names, 0, 0, &air->air_name );
514 if ( avl_insert( &attr_index, (caddr_t) air,
515 attr_index_cmp, avl_dup_error ) )
517 AttributeType *old_sat;
522 old_sat = at_bvfind( &air->air_name );
523 assert( old_sat != NULL );
524 rc = at_check_dup( old_sat, sat );
528 while ( names > sat->sat_names ) {
529 struct aindexrec tmpair;
532 ber_str2bv( *names, 0, 0, &tmpair.air_name );
534 air = (struct aindexrec *)avl_delete( &attr_index,
535 (caddr_t)&tmpair, attr_index_cmp );
536 assert( air != NULL );
540 if ( sat->sat_oid ) {
541 struct aindexrec tmpair;
543 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
545 air = (struct aindexrec *)avl_delete( &attr_index,
546 (caddr_t)&tmpair, attr_index_cmp );
547 assert( air != NULL );
553 /* FIX: temporal consistency check */
554 at_bvfind(&air->air_name);
559 if ( sat->sat_oid ) {
560 slap_ad_undef_promote( sat->sat_oid, sat );
563 names = sat->sat_names;
566 slap_ad_undef_promote( *names, sat );
571 if ( sat->sat_flags & SLAP_AT_HARDCODE ) {
576 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
578 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
586 LDAPAttributeType *at,
588 AttributeType **rsat,
592 AttributeType *sat = NULL;
593 MatchingRule *mr = NULL;
596 int code = LDAP_SUCCESS;
603 return SLAP_SCHERR_ATTR_INCOMPLETE;
606 if ( !OID_LEADCHAR( at->at_oid[0] )) {
609 /* Expand OID macros */
610 oid = oidm_find( at->at_oid );
613 return SLAP_SCHERR_OIDM;
615 if ( oid != at->at_oid ) {
621 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
624 /* Expand OID macros */
625 oid = oidm_find( at->at_syntax_oid );
627 *err = at->at_syntax_oid;
628 code = SLAP_SCHERR_OIDM;
631 if ( oid != at->at_syntax_oid ) {
632 soidm = at->at_syntax_oid;
633 at->at_syntax_oid = oid;
637 if ( at->at_names && at->at_names[0] ) {
640 for( i=0; at->at_names[i]; i++ ) {
641 if( !slap_valid_descr( at->at_names[i] ) ) {
642 *err = at->at_names[i];
643 code = SLAP_SCHERR_BAD_DESCR;
648 cname = at->at_names[0];
657 if ( !at->at_usage && at->at_no_user_mod ) {
658 /* user attribute must be modifable */
659 code = SLAP_SCHERR_ATTR_BAD_USAGE;
663 if ( at->at_collective ) {
665 /* collective attributes cannot be operational */
666 code = SLAP_SCHERR_ATTR_BAD_USAGE;
670 if( at->at_single_value ) {
671 /* collective attributes cannot be single-valued */
672 code = SLAP_SCHERR_ATTR_BAD_USAGE;
677 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
678 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
680 sat->sat_cname.bv_val = cname;
681 sat->sat_cname.bv_len = strlen( cname );
682 sat->sat_oidmacro = oidm;
683 sat->sat_soidmacro = soidm;
684 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
686 if ( at->at_sup_oid ) {
687 AttributeType *supsat = at_find(at->at_sup_oid);
689 if ( supsat == NULL ) {
690 *err = at->at_sup_oid;
691 code = SLAP_SCHERR_ATTR_NOT_FOUND;
695 sat->sat_sup = supsat;
697 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
698 code = SLAP_SCHERR_OUTOFMEM;
702 if ( sat->sat_usage != supsat->sat_usage ) {
703 /* subtypes must have same usage as their SUP */
704 code = SLAP_SCHERR_ATTR_BAD_USAGE;
708 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
709 /* subtypes must be obsolete if super is */
710 code = SLAP_SCHERR_ATTR_BAD_SUP;
714 if ( sat->sat_flags & SLAP_AT_FINAL ) {
715 /* cannot subtype a "final" attribute type */
716 code = SLAP_SCHERR_ATTR_BAD_SUP;
722 * Inherit definitions from superiors. We only check the
723 * direct superior since that one has already inherited from
726 if ( sat->sat_sup ) {
727 Syntax *syn = syn_find(sat->sat_sup->sat_syntax->ssyn_oid);
728 if ( syn != sat->sat_sup->sat_syntax ) {
729 sat->sat_syntax = ch_malloc( sizeof( Syntax ));
730 *sat->sat_syntax = *sat->sat_sup->sat_syntax;
732 sat->sat_syntax = sat->sat_sup->sat_syntax;
734 if ( sat->sat_sup->sat_equality ) {
735 MatchingRule *mr = mr_find( sat->sat_sup->sat_equality->smr_oid );
736 if ( mr != sat->sat_sup->sat_equality ) {
737 sat->sat_equality = ch_malloc( sizeof( MatchingRule ));
738 *sat->sat_equality = *sat->sat_sup->sat_equality;
740 sat->sat_equality = sat->sat_sup->sat_equality;
743 sat->sat_approx = sat->sat_sup->sat_approx;
744 sat->sat_ordering = sat->sat_sup->sat_ordering;
745 sat->sat_substr = sat->sat_sup->sat_substr;
749 * check for X-ORDERED attributes
751 if ( sat->sat_extensions ) {
752 for (i=0; sat->sat_extensions[i]; i++) {
753 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
754 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
755 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
757 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
759 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
761 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
769 sat->sat_flags |= SLAP_AT_HARDCODE;
771 if ( at->at_syntax_oid ) {
772 syn = syn_find(sat->sat_syntax_oid);
774 *err = sat->sat_syntax_oid;
775 code = SLAP_SCHERR_SYN_NOT_FOUND;
779 if ( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
780 /* BEWARE: no loop detection! */
781 if ( syn_is_sup( sat->sat_syntax, syn ) ) {
782 code = SLAP_SCHERR_ATTR_BAD_SUP;
787 sat->sat_syntax = syn;
789 } else if ( sat->sat_syntax == NULL ) {
790 code = SLAP_SCHERR_ATTR_INCOMPLETE;
794 if ( sat->sat_equality_oid ) {
795 mr = mr_find(sat->sat_equality_oid);
798 *err = sat->sat_equality_oid;
799 code = SLAP_SCHERR_MR_NOT_FOUND;
803 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
804 *err = sat->sat_equality_oid;
805 code = SLAP_SCHERR_ATTR_BAD_MR;
809 if( sat->sat_syntax != mr->smr_syntax ) {
810 if( mr->smr_compat_syntaxes == NULL ) {
811 *err = sat->sat_equality_oid;
812 code = SLAP_SCHERR_ATTR_BAD_MR;
816 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
817 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
824 *err = sat->sat_equality_oid;
825 code = SLAP_SCHERR_ATTR_BAD_MR;
830 sat->sat_equality = mr;
831 sat->sat_approx = mr->smr_associated;
834 if ( sat->sat_ordering_oid ) {
835 if( !sat->sat_equality ) {
836 *err = sat->sat_ordering_oid;
837 code = SLAP_SCHERR_ATTR_BAD_MR;
841 mr = mr_find(sat->sat_ordering_oid);
844 *err = sat->sat_ordering_oid;
845 code = SLAP_SCHERR_MR_NOT_FOUND;
849 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
850 *err = sat->sat_ordering_oid;
851 code = SLAP_SCHERR_ATTR_BAD_MR;
855 if( sat->sat_syntax != mr->smr_syntax ) {
856 if( mr->smr_compat_syntaxes == NULL ) {
857 *err = sat->sat_ordering_oid;
858 code = SLAP_SCHERR_ATTR_BAD_MR;
862 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
863 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
870 *err = sat->sat_ordering_oid;
871 code = SLAP_SCHERR_ATTR_BAD_MR;
876 sat->sat_ordering = mr;
879 if ( sat->sat_substr_oid ) {
880 if( !sat->sat_equality ) {
881 *err = sat->sat_substr_oid;
882 code = SLAP_SCHERR_ATTR_BAD_MR;
886 mr = mr_find(sat->sat_substr_oid);
889 *err = sat->sat_substr_oid;
890 code = SLAP_SCHERR_MR_NOT_FOUND;
894 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
895 *err = sat->sat_substr_oid;
896 code = SLAP_SCHERR_ATTR_BAD_MR;
900 /* due to funky LDAP builtin substring rules,
901 * we check against the equality rule assertion
902 * syntax and compat syntaxes instead of those
903 * associated with the substrings rule.
905 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
906 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
907 *err = sat->sat_substr_oid;
908 code = SLAP_SCHERR_ATTR_BAD_MR;
912 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
913 if( sat->sat_syntax ==
914 sat->sat_equality->smr_compat_syntaxes[i] )
922 *err = sat->sat_substr_oid;
923 code = SLAP_SCHERR_ATTR_BAD_MR;
928 sat->sat_substr = mr;
931 code = at_insert( &sat, prev, err );
935 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
940 SLAP_FREE( at->at_oid );
945 SLAP_FREE( at->at_syntax_oid );
946 at->at_syntax_oid = soidm;
959 at_index_printnode( void *v_air, void *ignore )
961 struct aindexrec *air = v_air;
963 air->air_name.bv_val,
964 ldap_attributetype2str(&air->air_at->sat_atype) );
969 at_index_print( void )
971 printf("Printing attribute type index:\n");
972 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
978 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
982 struct berval bv, *bva = NULL, idx;
986 start = LDAP_STAILQ_FIRST( &attr_list );
988 /* count the result size */
990 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
991 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
993 if ( at == end ) break;
998 bva = ch_malloc( (num+1) * sizeof(struct berval) );
1006 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
1007 LDAPAttributeType lat, *latp;
1008 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
1009 if ( at->sat_oidmacro || at->sat_soidmacro ) {
1010 lat = at->sat_atype;
1011 if ( at->sat_oidmacro )
1012 lat.at_oid = at->sat_oidmacro;
1013 if ( at->sat_soidmacro )
1014 lat.at_syntax_oid = at->sat_soidmacro;
1017 latp = &at->sat_atype;
1019 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
1020 ber_bvarray_free( bva );
1023 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
1025 bva[i].bv_len = idx.bv_len + bv.bv_len;
1026 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1027 strcpy( bva[i].bv_val, ibuf );
1028 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
1030 bva[i].bv_val = NULL;
1031 ldap_memfree( bv.bv_val );
1032 if ( at == end ) break;
1038 at_schema_info( Entry *e )
1040 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
1045 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
1046 if( at->sat_flags & SLAP_AT_HIDE ) continue;
1048 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
1052 ber_str2bv( at->sat_oid, 0, 0, &nval );
1054 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
1058 ldap_memfree( val.bv_val );
1064 register_at( const char *def, AttributeDescription **rad, int dupok )
1066 LDAPAttributeType *at;
1067 int code, freeit = 0;
1069 AttributeDescription *ad = NULL;
1071 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1073 Debug( LDAP_DEBUG_ANY,
1074 "register_at: AttributeType \"%s\": %s, %s\n",
1075 def, ldap_scherr2str(code), err );
1079 code = at_add( at, 0, NULL, NULL, &err );
1081 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1085 Debug( LDAP_DEBUG_ANY,
1086 "register_at: AttributeType \"%s\": %s, %s\n",
1087 def, scherr2str(code), err );
1088 ldap_attributetype_free( at );
1092 code = slap_str2ad( at->at_names[0], &ad, &err );
1093 if ( freeit || code ) {
1094 ldap_attributetype_free( at );
1099 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1102 if ( rad ) *rad = ad;