1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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;
236 struct aindexrec tmpair, *air;
238 ber_str2bv( *names, 0, 0, &tmpair.air_name );
240 air = (struct aindexrec *)avl_delete( &attr_index,
241 (caddr_t)&tmpair, attr_index_cmp );
242 assert( air != NULL );
248 /* Mark the attribute as deleted, remove from list, and remove all its
249 * names from the AVL tree. Leave the OID in the tree.
252 at_delete( AttributeType *at )
254 at->sat_flags |= SLAP_AT_DELETED;
256 LDAP_STAILQ_REMOVE(&attr_list, at, AttributeType, sat_next);
258 at_delete_names( at );
262 at_clean( AttributeType *a )
264 if ( a->sat_equality ) {
267 mr = mr_find( a->sat_equality->smr_oid );
268 assert( mr != NULL );
269 if ( mr != a->sat_equality ) {
270 ch_free( a->sat_equality );
271 a->sat_equality = NULL;
275 assert( a->sat_syntax != NULL );
276 if ( a->sat_syntax != NULL ) {
279 syn = syn_find( a->sat_syntax->ssyn_oid );
280 assert( syn != NULL );
281 if ( syn != a->sat_syntax ) {
282 ch_free( a->sat_syntax );
283 a->sat_syntax = NULL;
287 if ( a->sat_oidmacro ) {
288 ldap_memfree( a->sat_oidmacro );
289 a->sat_oidmacro = NULL;
291 if ( a->sat_subtypes ) {
292 ldap_memfree( a->sat_subtypes );
293 a->sat_subtypes = NULL;
298 at_destroy_one( void *v )
300 struct aindexrec *air = v;
301 AttributeType *a = air->air_at;
304 ad_destroy(a->sat_ad);
305 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
306 ldap_attributetype_free((LDAPAttributeType *)a);
315 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
316 a = LDAP_STAILQ_FIRST(&attr_list);
317 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
319 at_delete_names( a );
322 avl_free(attr_index, at_destroy_one);
324 if ( slap_schema.si_at_undefined ) {
325 ad_destroy(slap_schema.si_at_undefined->sat_ad);
328 if ( slap_schema.si_at_proxied ) {
329 ad_destroy(slap_schema.si_at_proxied->sat_ad);
334 at_start( AttributeType **at )
336 assert( at != NULL );
338 *at = LDAP_STAILQ_FIRST(&attr_list);
340 return (*at != NULL);
344 at_next( AttributeType **at )
346 assert( at != NULL );
348 #if 0 /* pedantic check: don't use this */
350 AttributeType *tmp = NULL;
352 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
358 assert( tmp != NULL );
362 *at = LDAP_STAILQ_NEXT(*at,sat_next);
364 return (*at != NULL);
368 * check whether the two attributeTypes actually __are__ identical,
369 * or rather inconsistent
374 AttributeType *new_sat )
376 if ( new_sat->sat_oid != NULL ) {
377 if ( sat->sat_oid == NULL ) {
378 return SLAP_SCHERR_ATTR_INCONSISTENT;
381 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
382 return SLAP_SCHERR_ATTR_INCONSISTENT;
386 if ( sat->sat_oid != NULL ) {
387 return SLAP_SCHERR_ATTR_INCONSISTENT;
391 if ( new_sat->sat_names ) {
394 if ( sat->sat_names == NULL ) {
395 return SLAP_SCHERR_ATTR_INCONSISTENT;
398 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
399 if ( sat->sat_names[ i ] == NULL ) {
400 return SLAP_SCHERR_ATTR_INCONSISTENT;
403 if ( strcasecmp( sat->sat_names[ i ],
404 new_sat->sat_names[ i ] ) != 0 )
406 return SLAP_SCHERR_ATTR_INCONSISTENT;
410 if ( sat->sat_names != NULL ) {
411 return SLAP_SCHERR_ATTR_INCONSISTENT;
415 return SLAP_SCHERR_ATTR_DUP;
418 static struct aindexrec *air_old;
421 at_dup_error( void *left, void *right )
433 struct aindexrec *air;
435 AttributeType *sat = *rat;
437 if ( sat->sat_oid ) {
438 air = (struct aindexrec *)
439 ch_calloc( 1, sizeof(struct aindexrec) );
440 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
444 if ( avl_insert( &attr_index, (caddr_t) air,
445 attr_index_cmp, at_dup_error ) )
447 AttributeType *old_sat;
452 assert( air_old != NULL );
453 old_sat = air_old->air_at;
455 /* replacing a deleted definition? */
456 if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
458 AttributeDescription *ad;
460 /* Keep old oid, free new oid;
461 * Keep old ads, free new ads;
462 * Keep old ad_mutex, free new ad_mutex;
463 * Keep new everything else, free old
467 old_sat->sat_oid = tmp.sat_oid;
468 tmp.sat_oid = sat->sat_oid;
469 old_sat->sat_ad = tmp.sat_ad;
470 tmp.sat_ad = sat->sat_ad;
471 old_sat->sat_ad_mutex = tmp.sat_ad_mutex;
472 tmp.sat_ad_mutex = sat->sat_ad_mutex;
475 /* Check for basic ad pointing at old cname */
476 for ( ad = old_sat->sat_ad; ad; ad=ad->ad_next ) {
477 if ( ad->ad_cname.bv_val == sat->sat_cname.bv_val ) {
478 ad->ad_cname = old_sat->sat_cname;
484 at_destroy_one( air );
492 rc = at_check_dup( old_sat, sat );
497 /* FIX: temporal consistency check */
498 at_bvfind( &air->air_name );
501 names = sat->sat_names;
504 air = (struct aindexrec *)
505 ch_calloc( 1, sizeof(struct aindexrec) );
506 ber_str2bv( *names, 0, 0, &air->air_name );
508 if ( avl_insert( &attr_index, (caddr_t) air,
509 attr_index_cmp, avl_dup_error ) )
511 AttributeType *old_sat;
516 old_sat = at_bvfind( &air->air_name );
517 assert( old_sat != NULL );
518 rc = at_check_dup( old_sat, sat );
522 while ( names > sat->sat_names ) {
523 struct aindexrec tmpair;
526 ber_str2bv( *names, 0, 0, &tmpair.air_name );
528 air = (struct aindexrec *)avl_delete( &attr_index,
529 (caddr_t)&tmpair, attr_index_cmp );
530 assert( air != NULL );
534 if ( sat->sat_oid ) {
535 struct aindexrec tmpair;
537 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
539 air = (struct aindexrec *)avl_delete( &attr_index,
540 (caddr_t)&tmpair, attr_index_cmp );
541 assert( air != NULL );
547 /* FIX: temporal consistency check */
548 at_bvfind(&air->air_name);
553 if ( sat->sat_oid ) {
554 slap_ad_undef_promote( sat->sat_oid, sat );
557 names = sat->sat_names;
560 slap_ad_undef_promote( *names, sat );
565 if ( sat->sat_flags & SLAP_AT_HARDCODE ) {
570 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
572 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
580 LDAPAttributeType *at,
582 AttributeType **rsat,
586 AttributeType *sat = NULL;
587 MatchingRule *mr = NULL;
590 int code = LDAP_SUCCESS;
596 return SLAP_SCHERR_ATTR_INCOMPLETE;
599 if ( !OID_LEADCHAR( at->at_oid[0] )) {
602 /* Expand OID macros */
603 oid = oidm_find( at->at_oid );
606 return SLAP_SCHERR_OIDM;
608 if ( oid != at->at_oid ) {
614 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
617 /* Expand OID macros */
618 oid = oidm_find( at->at_syntax_oid );
620 *err = at->at_syntax_oid;
621 code = SLAP_SCHERR_OIDM;
624 if ( oid != at->at_syntax_oid ) {
625 ldap_memfree( at->at_syntax_oid );
626 at->at_syntax_oid = oid;
630 if ( at->at_names && at->at_names[0] ) {
633 for( i=0; at->at_names[i]; i++ ) {
634 if( !slap_valid_descr( at->at_names[i] ) ) {
635 *err = at->at_names[i];
636 code = SLAP_SCHERR_BAD_DESCR;
641 cname = at->at_names[0];
650 if ( !at->at_usage && at->at_no_user_mod ) {
651 /* user attribute must be modifable */
652 code = SLAP_SCHERR_ATTR_BAD_USAGE;
656 if ( at->at_collective ) {
658 /* collective attributes cannot be operational */
659 code = SLAP_SCHERR_ATTR_BAD_USAGE;
663 if( at->at_single_value ) {
664 /* collective attributes cannot be single-valued */
665 code = SLAP_SCHERR_ATTR_BAD_USAGE;
670 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
671 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
673 sat->sat_cname.bv_val = cname;
674 sat->sat_cname.bv_len = strlen( cname );
675 sat->sat_oidmacro = oidm;
676 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
678 if ( at->at_sup_oid ) {
679 AttributeType *supsat = at_find(at->at_sup_oid);
681 if ( supsat == NULL ) {
682 *err = at->at_sup_oid;
683 code = SLAP_SCHERR_ATTR_NOT_FOUND;
687 sat->sat_sup = supsat;
689 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
690 code = SLAP_SCHERR_OUTOFMEM;
694 if ( sat->sat_usage != supsat->sat_usage ) {
695 /* subtypes must have same usage as their SUP */
696 code = SLAP_SCHERR_ATTR_BAD_USAGE;
700 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
701 /* subtypes must be obsolete if super is */
702 code = SLAP_SCHERR_ATTR_BAD_SUP;
706 if ( sat->sat_flags & SLAP_AT_FINAL ) {
707 /* cannot subtype a "final" attribute type */
708 code = SLAP_SCHERR_ATTR_BAD_SUP;
714 * Inherit definitions from superiors. We only check the
715 * direct superior since that one has already inherited from
718 if ( sat->sat_sup ) {
719 sat->sat_syntax = sat->sat_sup->sat_syntax;
720 sat->sat_equality = sat->sat_sup->sat_equality;
721 sat->sat_approx = sat->sat_sup->sat_approx;
722 sat->sat_ordering = sat->sat_sup->sat_ordering;
723 sat->sat_substr = sat->sat_sup->sat_substr;
727 * check for X-ORDERED attributes
729 if ( sat->sat_extensions ) {
730 for (i=0; sat->sat_extensions[i]; i++) {
731 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
732 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
733 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
735 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
737 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
739 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
747 sat->sat_flags |= SLAP_AT_HARDCODE;
749 if ( at->at_syntax_oid ) {
750 syn = syn_find(sat->sat_syntax_oid);
752 *err = sat->sat_syntax_oid;
753 code = SLAP_SCHERR_SYN_NOT_FOUND;
757 if ( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
758 /* BEWARE: no loop detection! */
759 if ( syn_is_sup( sat->sat_syntax, syn ) ) {
760 code = SLAP_SCHERR_ATTR_BAD_SUP;
765 sat->sat_syntax = syn;
767 } else if ( sat->sat_syntax == NULL ) {
768 code = SLAP_SCHERR_ATTR_INCOMPLETE;
772 if ( sat->sat_equality_oid ) {
773 mr = mr_find(sat->sat_equality_oid);
776 *err = sat->sat_equality_oid;
777 code = SLAP_SCHERR_MR_NOT_FOUND;
781 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
782 *err = sat->sat_equality_oid;
783 code = SLAP_SCHERR_ATTR_BAD_MR;
787 if( sat->sat_syntax != mr->smr_syntax ) {
788 if( mr->smr_compat_syntaxes == NULL ) {
789 *err = sat->sat_equality_oid;
790 code = SLAP_SCHERR_ATTR_BAD_MR;
794 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
795 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
802 *err = sat->sat_equality_oid;
803 code = SLAP_SCHERR_ATTR_BAD_MR;
808 sat->sat_equality = mr;
809 sat->sat_approx = mr->smr_associated;
812 if ( sat->sat_ordering_oid ) {
813 if( !sat->sat_equality ) {
814 *err = sat->sat_ordering_oid;
815 code = SLAP_SCHERR_ATTR_BAD_MR;
819 mr = mr_find(sat->sat_ordering_oid);
822 *err = sat->sat_ordering_oid;
823 code = SLAP_SCHERR_MR_NOT_FOUND;
827 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
828 *err = sat->sat_ordering_oid;
829 code = SLAP_SCHERR_ATTR_BAD_MR;
833 if( sat->sat_syntax != mr->smr_syntax ) {
834 if( mr->smr_compat_syntaxes == NULL ) {
835 *err = sat->sat_ordering_oid;
836 code = SLAP_SCHERR_ATTR_BAD_MR;
840 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
841 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
848 *err = sat->sat_ordering_oid;
849 code = SLAP_SCHERR_ATTR_BAD_MR;
854 sat->sat_ordering = mr;
857 if ( sat->sat_substr_oid ) {
858 if( !sat->sat_equality ) {
859 *err = sat->sat_substr_oid;
860 code = SLAP_SCHERR_ATTR_BAD_MR;
864 mr = mr_find(sat->sat_substr_oid);
867 *err = sat->sat_substr_oid;
868 code = SLAP_SCHERR_MR_NOT_FOUND;
872 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
873 *err = sat->sat_substr_oid;
874 code = SLAP_SCHERR_ATTR_BAD_MR;
878 /* due to funky LDAP builtin substring rules,
879 * we check against the equality rule assertion
880 * syntax and compat syntaxes instead of those
881 * associated with the substrings rule.
883 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
884 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
885 *err = sat->sat_substr_oid;
886 code = SLAP_SCHERR_ATTR_BAD_MR;
890 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
891 if( sat->sat_syntax ==
892 sat->sat_equality->smr_compat_syntaxes[i] )
900 *err = sat->sat_substr_oid;
901 code = SLAP_SCHERR_ATTR_BAD_MR;
906 sat->sat_substr = mr;
909 code = at_insert( &sat, prev, err );
913 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
918 SLAP_FREE( at->at_oid );
932 at_index_printnode( void *v_air, void *ignore )
934 struct aindexrec *air = v_air;
936 air->air_name.bv_val,
937 ldap_attributetype2str(&air->air_at->sat_atype) );
942 at_index_print( void )
944 printf("Printing attribute type index:\n");
945 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
951 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
955 struct berval bv, *bva = NULL, idx;
959 start = LDAP_STAILQ_FIRST( &attr_list );
961 /* count the result size */
963 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
964 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
966 if ( at == end ) break;
971 bva = ch_malloc( (num+1) * sizeof(struct berval) );
979 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
980 LDAPAttributeType lat, *latp;
981 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
982 if ( at->sat_oidmacro ) {
984 lat.at_oid = at->sat_oidmacro;
987 latp = &at->sat_atype;
989 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
990 ber_bvarray_free( bva );
993 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
995 bva[i].bv_len = idx.bv_len + bv.bv_len;
996 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
997 strcpy( bva[i].bv_val, ibuf );
998 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
1000 bva[i].bv_val = NULL;
1001 ldap_memfree( bv.bv_val );
1002 if ( at == end ) break;
1008 at_schema_info( Entry *e )
1010 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
1015 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
1016 if( at->sat_flags & SLAP_AT_HIDE ) continue;
1018 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
1022 ber_str2bv( at->sat_oid, 0, 0, &nval );
1024 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
1028 ldap_memfree( val.bv_val );
1034 register_at( char *def, AttributeDescription **rad, int dupok )
1036 LDAPAttributeType *at;
1037 int code, freeit = 0;
1039 AttributeDescription *ad = NULL;
1041 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1043 Debug( LDAP_DEBUG_ANY,
1044 "register_at: AttributeType \"%s\": %s, %s\n",
1045 def, ldap_scherr2str(code), err );
1049 code = at_add( at, 0, NULL, NULL, &err );
1051 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1055 ldap_attributetype_free( at );
1056 Debug( LDAP_DEBUG_ANY,
1057 "register_at: AttributeType \"%s\": %s, %s\n",
1058 def, scherr2str(code), err );
1062 code = slap_str2ad( at->at_names[0], &ad, &err );
1063 if ( freeit || code ) {
1064 ldap_attributetype_free( at );
1069 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1072 if ( rad ) *rad = ad;