1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2016 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 );
372 *at = LDAP_STAILQ_NEXT(*at,sat_next);
374 return (*at != NULL);
378 * check whether the two attributeTypes actually __are__ identical,
379 * or rather inconsistent
384 AttributeType *new_sat )
386 if ( new_sat->sat_oid != NULL ) {
387 if ( sat->sat_oid == NULL ) {
388 return SLAP_SCHERR_ATTR_INCONSISTENT;
391 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
392 return SLAP_SCHERR_ATTR_INCONSISTENT;
396 if ( sat->sat_oid != NULL ) {
397 return SLAP_SCHERR_ATTR_INCONSISTENT;
401 if ( new_sat->sat_names ) {
404 if ( sat->sat_names == NULL ) {
405 return SLAP_SCHERR_ATTR_INCONSISTENT;
408 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
409 if ( sat->sat_names[ i ] == NULL ) {
410 return SLAP_SCHERR_ATTR_INCONSISTENT;
413 if ( strcasecmp( sat->sat_names[ i ],
414 new_sat->sat_names[ i ] ) != 0 )
416 return SLAP_SCHERR_ATTR_INCONSISTENT;
420 if ( sat->sat_names != NULL ) {
421 return SLAP_SCHERR_ATTR_INCONSISTENT;
425 return SLAP_SCHERR_ATTR_DUP;
428 static struct aindexrec *air_old;
431 at_dup_error( void *left, void *right )
443 struct aindexrec *air;
445 AttributeType *sat = *rat;
447 if ( sat->sat_oid ) {
448 air = (struct aindexrec *)
449 ch_calloc( 1, sizeof(struct aindexrec) );
450 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
454 if ( avl_insert( &attr_index, (caddr_t) air,
455 attr_index_cmp, at_dup_error ) )
457 AttributeType *old_sat;
462 assert( air_old != NULL );
463 old_sat = air_old->air_at;
465 /* replacing a deleted definition? */
466 if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
468 AttributeDescription *ad;
470 /* Keep old oid, free new oid;
471 * Keep old ads, free new ads;
472 * Keep old ad_mutex, free new ad_mutex;
473 * Keep new everything else, free old
477 old_sat->sat_oid = tmp.sat_oid;
478 tmp.sat_oid = sat->sat_oid;
479 old_sat->sat_ad = tmp.sat_ad;
480 tmp.sat_ad = sat->sat_ad;
481 old_sat->sat_ad_mutex = tmp.sat_ad_mutex;
482 tmp.sat_ad_mutex = sat->sat_ad_mutex;
485 /* Check for basic ad pointing at old cname */
486 for ( ad = old_sat->sat_ad; ad; ad=ad->ad_next ) {
487 if ( ad->ad_cname.bv_val == sat->sat_cname.bv_val ) {
488 ad->ad_cname = old_sat->sat_cname;
494 at_destroy_one( air );
502 rc = at_check_dup( old_sat, sat );
507 /* FIX: temporal consistency check */
508 at_bvfind( &air->air_name );
511 names = sat->sat_names;
514 air = (struct aindexrec *)
515 ch_calloc( 1, sizeof(struct aindexrec) );
516 ber_str2bv( *names, 0, 0, &air->air_name );
518 if ( avl_insert( &attr_index, (caddr_t) air,
519 attr_index_cmp, avl_dup_error ) )
521 AttributeType *old_sat;
526 old_sat = at_bvfind( &air->air_name );
527 assert( old_sat != NULL );
528 rc = at_check_dup( old_sat, sat );
532 while ( names > sat->sat_names ) {
533 struct aindexrec tmpair;
536 ber_str2bv( *names, 0, 0, &tmpair.air_name );
538 air = (struct aindexrec *)avl_delete( &attr_index,
539 (caddr_t)&tmpair, attr_index_cmp );
540 assert( air != NULL );
544 if ( sat->sat_oid ) {
545 struct aindexrec tmpair;
547 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
549 air = (struct aindexrec *)avl_delete( &attr_index,
550 (caddr_t)&tmpair, attr_index_cmp );
551 assert( air != NULL );
557 /* FIX: temporal consistency check */
558 at_bvfind(&air->air_name);
563 if ( sat->sat_oid ) {
564 slap_ad_undef_promote( sat->sat_oid, sat );
567 names = sat->sat_names;
570 slap_ad_undef_promote( *names, sat );
575 if ( sat->sat_flags & SLAP_AT_HARDCODE ) {
580 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
582 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
590 LDAPAttributeType *at,
592 AttributeType **rsat,
596 AttributeType *sat = NULL;
597 MatchingRule *mr = NULL;
600 int code = LDAP_SUCCESS;
607 return SLAP_SCHERR_ATTR_INCOMPLETE;
610 if ( !OID_LEADCHAR( at->at_oid[0] )) {
613 /* Expand OID macros */
614 oid = oidm_find( at->at_oid );
617 return SLAP_SCHERR_OIDM;
619 if ( oid != at->at_oid ) {
625 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
628 /* Expand OID macros */
629 oid = oidm_find( at->at_syntax_oid );
631 *err = at->at_syntax_oid;
632 code = SLAP_SCHERR_OIDM;
635 if ( oid != at->at_syntax_oid ) {
636 soidm = at->at_syntax_oid;
637 at->at_syntax_oid = oid;
641 if ( at->at_names && at->at_names[0] ) {
644 for( i=0; at->at_names[i]; i++ ) {
645 if( !slap_valid_descr( at->at_names[i] ) ) {
646 *err = at->at_names[i];
647 code = SLAP_SCHERR_BAD_DESCR;
652 cname = at->at_names[0];
661 if ( !at->at_usage && at->at_no_user_mod ) {
662 /* user attribute must be modifable */
663 code = SLAP_SCHERR_ATTR_BAD_USAGE;
667 if ( at->at_collective ) {
669 /* collective attributes cannot be operational */
670 code = SLAP_SCHERR_ATTR_BAD_USAGE;
674 if( at->at_single_value ) {
675 /* collective attributes cannot be single-valued */
676 code = SLAP_SCHERR_ATTR_BAD_USAGE;
681 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
682 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
684 sat->sat_cname.bv_val = cname;
685 sat->sat_cname.bv_len = strlen( cname );
686 sat->sat_oidmacro = oidm;
687 sat->sat_soidmacro = soidm;
688 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
690 if ( at->at_sup_oid ) {
691 AttributeType *supsat = at_find(at->at_sup_oid);
693 if ( supsat == NULL ) {
694 *err = at->at_sup_oid;
695 code = SLAP_SCHERR_ATTR_NOT_FOUND;
699 sat->sat_sup = supsat;
701 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
702 code = SLAP_SCHERR_OUTOFMEM;
706 if ( sat->sat_usage != supsat->sat_usage ) {
707 /* subtypes must have same usage as their SUP */
708 code = SLAP_SCHERR_ATTR_BAD_USAGE;
712 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
713 /* subtypes must be obsolete if super is */
714 code = SLAP_SCHERR_ATTR_BAD_SUP;
718 if ( sat->sat_flags & SLAP_AT_FINAL ) {
719 /* cannot subtype a "final" attribute type */
720 code = SLAP_SCHERR_ATTR_BAD_SUP;
726 * Inherit definitions from superiors. We only check the
727 * direct superior since that one has already inherited from
730 if ( sat->sat_sup ) {
731 Syntax *syn = syn_find(sat->sat_sup->sat_syntax->ssyn_oid);
732 if ( syn != sat->sat_sup->sat_syntax ) {
733 sat->sat_syntax = ch_malloc( sizeof( Syntax ));
734 *sat->sat_syntax = *sat->sat_sup->sat_syntax;
736 sat->sat_syntax = sat->sat_sup->sat_syntax;
738 if ( sat->sat_sup->sat_equality ) {
739 MatchingRule *mr = mr_find( sat->sat_sup->sat_equality->smr_oid );
740 if ( mr != sat->sat_sup->sat_equality ) {
741 sat->sat_equality = ch_malloc( sizeof( MatchingRule ));
742 *sat->sat_equality = *sat->sat_sup->sat_equality;
744 sat->sat_equality = sat->sat_sup->sat_equality;
747 sat->sat_approx = sat->sat_sup->sat_approx;
748 sat->sat_ordering = sat->sat_sup->sat_ordering;
749 sat->sat_substr = sat->sat_sup->sat_substr;
753 * check for X-ORDERED attributes
755 if ( sat->sat_extensions ) {
756 for (i=0; sat->sat_extensions[i]; i++) {
757 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
758 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
759 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
761 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
763 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
765 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
773 sat->sat_flags |= SLAP_AT_HARDCODE;
775 if ( at->at_syntax_oid ) {
776 syn = syn_find(sat->sat_syntax_oid);
778 *err = sat->sat_syntax_oid;
779 code = SLAP_SCHERR_SYN_NOT_FOUND;
783 if ( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
784 /* BEWARE: no loop detection! */
785 if ( syn_is_sup( sat->sat_syntax, syn ) ) {
786 code = SLAP_SCHERR_ATTR_BAD_SUP;
791 sat->sat_syntax = syn;
793 } else if ( sat->sat_syntax == NULL ) {
794 code = SLAP_SCHERR_ATTR_INCOMPLETE;
798 if ( sat->sat_equality_oid ) {
799 mr = mr_find(sat->sat_equality_oid);
802 *err = sat->sat_equality_oid;
803 code = SLAP_SCHERR_MR_NOT_FOUND;
807 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
808 *err = sat->sat_equality_oid;
809 code = SLAP_SCHERR_ATTR_BAD_MR;
813 if( sat->sat_syntax != mr->smr_syntax ) {
814 if( mr->smr_compat_syntaxes == NULL ) {
815 *err = sat->sat_equality_oid;
816 code = SLAP_SCHERR_ATTR_BAD_MR;
820 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
821 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
828 *err = sat->sat_equality_oid;
829 code = SLAP_SCHERR_ATTR_BAD_MR;
834 sat->sat_equality = mr;
835 sat->sat_approx = mr->smr_associated;
838 if ( sat->sat_ordering_oid ) {
839 if( !sat->sat_equality ) {
840 *err = sat->sat_ordering_oid;
841 code = SLAP_SCHERR_ATTR_BAD_MR;
845 mr = mr_find(sat->sat_ordering_oid);
848 *err = sat->sat_ordering_oid;
849 code = SLAP_SCHERR_MR_NOT_FOUND;
853 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
854 *err = sat->sat_ordering_oid;
855 code = SLAP_SCHERR_ATTR_BAD_MR;
859 if( sat->sat_syntax != mr->smr_syntax ) {
860 if( mr->smr_compat_syntaxes == NULL ) {
861 *err = sat->sat_ordering_oid;
862 code = SLAP_SCHERR_ATTR_BAD_MR;
866 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
867 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
874 *err = sat->sat_ordering_oid;
875 code = SLAP_SCHERR_ATTR_BAD_MR;
880 sat->sat_ordering = mr;
883 if ( sat->sat_substr_oid ) {
884 if( !sat->sat_equality ) {
885 *err = sat->sat_substr_oid;
886 code = SLAP_SCHERR_ATTR_BAD_MR;
890 mr = mr_find(sat->sat_substr_oid);
893 *err = sat->sat_substr_oid;
894 code = SLAP_SCHERR_MR_NOT_FOUND;
898 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
899 *err = sat->sat_substr_oid;
900 code = SLAP_SCHERR_ATTR_BAD_MR;
904 /* due to funky LDAP builtin substring rules,
905 * we check against the equality rule assertion
906 * syntax and compat syntaxes instead of those
907 * associated with the substrings rule.
909 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
910 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
911 *err = sat->sat_substr_oid;
912 code = SLAP_SCHERR_ATTR_BAD_MR;
916 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
917 if( sat->sat_syntax ==
918 sat->sat_equality->smr_compat_syntaxes[i] )
926 *err = sat->sat_substr_oid;
927 code = SLAP_SCHERR_ATTR_BAD_MR;
932 sat->sat_substr = mr;
935 code = at_insert( &sat, prev, err );
939 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
944 SLAP_FREE( at->at_oid );
949 SLAP_FREE( at->at_syntax_oid );
950 at->at_syntax_oid = soidm;
963 at_index_printnode( void *v_air, void *ignore )
965 struct aindexrec *air = v_air;
967 air->air_name.bv_val,
968 ldap_attributetype2str(&air->air_at->sat_atype) );
973 at_index_print( void )
975 printf("Printing attribute type index:\n");
976 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
982 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
986 struct berval bv, *bva = NULL, idx;
990 start = LDAP_STAILQ_FIRST( &attr_list );
992 /* count the result size */
994 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
995 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
997 if ( at == end ) break;
1002 bva = ch_malloc( (num+1) * sizeof(struct berval) );
1010 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
1011 LDAPAttributeType lat, *latp;
1012 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
1013 if ( at->sat_oidmacro || at->sat_soidmacro ) {
1014 lat = at->sat_atype;
1015 if ( at->sat_oidmacro )
1016 lat.at_oid = at->sat_oidmacro;
1017 if ( at->sat_soidmacro )
1018 lat.at_syntax_oid = at->sat_soidmacro;
1021 latp = &at->sat_atype;
1023 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
1024 ber_bvarray_free( bva );
1027 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
1029 bva[i].bv_len = idx.bv_len + bv.bv_len;
1030 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1031 strcpy( bva[i].bv_val, ibuf );
1032 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
1034 bva[i].bv_val = NULL;
1035 ldap_memfree( bv.bv_val );
1036 if ( at == end ) break;
1042 at_schema_info( Entry *e )
1044 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
1049 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
1050 if( at->sat_flags & SLAP_AT_HIDE ) continue;
1052 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
1056 ber_str2bv( at->sat_oid, 0, 0, &nval );
1058 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
1062 ldap_memfree( val.bv_val );
1068 register_at( const char *def, AttributeDescription **rad, int dupok )
1070 LDAPAttributeType *at;
1071 int code, freeit = 0;
1073 AttributeDescription *ad = NULL;
1075 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1077 Debug( LDAP_DEBUG_ANY,
1078 "register_at: AttributeType \"%s\": %s, %s\n",
1079 def, ldap_scherr2str(code), err );
1083 code = at_add( at, 0, NULL, NULL, &err );
1085 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1089 Debug( LDAP_DEBUG_ANY,
1090 "register_at: AttributeType \"%s\": %s, %s\n",
1091 def, scherr2str(code), err );
1092 ldap_attributetype_free( at );
1096 code = slap_str2ad( at->at_names[0], &ad, &err );
1097 if ( freeit || code ) {
1098 ldap_attributetype_free( at );
1103 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1106 if ( rad ) *rad = ad;