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 );
114 if ( air->air_at->sat_flags & SLAP_AT_DELETED ) {
116 } else if (( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
117 avl_insert( &attr_cache, (caddr_t) air,
118 attr_index_cmp, avl_dup_error );
122 return air != NULL ? air->air_at : NULL;
128 AttributeType ***listp )
130 AttributeType **list;
131 AttributeType **list1;
137 list = ch_calloc(size, sizeof(AttributeType *));
149 list1 = ch_realloc(list, size*sizeof(AttributeType *));
164 AttributeType ***listp )
166 AttributeType **list;
167 AttributeType **list1;
175 for ( i=0; list[i]; i++ )
180 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
184 /* Tell the runtime this can be shrinked */
185 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
196 AttributeType **list )
203 for ( i=0; list[i]; i++ ) {
204 if ( sat == list[i] ) {
212 at_delete_names( AttributeType *at )
214 char **names = at->sat_names;
217 struct aindexrec tmpair, *air;
219 ber_str2bv( *names, 0, 0, &tmpair.air_name );
221 air = (struct aindexrec *)avl_delete( &attr_index,
222 (caddr_t)&tmpair, attr_index_cmp );
223 assert( air != NULL );
229 /* Mark the attribute as deleted, remove from list, and remove all its
230 * names from the AVL tree. Leave the OID in the tree.
233 at_delete( AttributeType *at )
235 at->sat_flags |= SLAP_AT_DELETED;
237 LDAP_STAILQ_REMOVE(&attr_list,at,slap_attribute_type,sat_next);
239 at_delete_names( at );
245 at_clean( AttributeType *a )
247 if ( a->sat_equality ) {
250 mr = mr_find( a->sat_equality->smr_oid );
251 assert( mr != NULL );
252 if ( mr != a->sat_equality ) {
253 ch_free( a->sat_equality );
254 a->sat_equality = NULL;
258 assert( a->sat_syntax != NULL );
259 if ( a->sat_syntax != NULL ) {
262 syn = syn_find( a->sat_syntax->ssyn_oid );
263 assert( syn != NULL );
264 if ( syn != a->sat_syntax ) {
265 ch_free( a->sat_syntax );
266 a->sat_syntax = NULL;
270 if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
271 if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
275 at_destroy_one( void *v )
277 struct aindexrec *air = v;
278 AttributeType *a = air->air_at;
281 ad_destroy(a->sat_ad);
282 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
283 ldap_attributetype_free((LDAPAttributeType *)a);
292 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
293 a = LDAP_STAILQ_FIRST(&attr_list);
294 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
296 at_delete_names( a );
299 avl_free(attr_index, at_destroy_one);
301 if ( slap_schema.si_at_undefined ) {
302 ad_destroy(slap_schema.si_at_undefined->sat_ad);
305 if ( slap_schema.si_at_proxied ) {
306 ad_destroy(slap_schema.si_at_proxied->sat_ad);
311 at_start( AttributeType **at )
313 assert( at != NULL );
315 *at = LDAP_STAILQ_FIRST(&attr_list);
317 return (*at != NULL);
321 at_next( AttributeType **at )
323 assert( at != NULL );
325 #if 1 /* pedantic check */
327 AttributeType *tmp = NULL;
329 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
335 assert( tmp != NULL );
339 *at = LDAP_STAILQ_NEXT(*at,sat_next);
341 return (*at != NULL);
345 * check whether the two attributeTypes actually __are__ identical,
346 * or rather inconsistent
351 AttributeType *new_sat )
353 if ( new_sat->sat_oid != NULL ) {
354 if ( sat->sat_oid == NULL ) {
355 return SLAP_SCHERR_ATTR_INCONSISTENT;
358 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
359 return SLAP_SCHERR_ATTR_INCONSISTENT;
363 if ( sat->sat_oid != NULL ) {
364 return SLAP_SCHERR_ATTR_INCONSISTENT;
368 if ( new_sat->sat_names ) {
371 if ( sat->sat_names == NULL ) {
372 return SLAP_SCHERR_ATTR_INCONSISTENT;
375 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
376 if ( sat->sat_names[ i ] == NULL ) {
377 return SLAP_SCHERR_ATTR_INCONSISTENT;
380 if ( strcasecmp( sat->sat_names[ i ],
381 new_sat->sat_names[ i ] ) != 0 )
383 return SLAP_SCHERR_ATTR_INCONSISTENT;
387 if ( sat->sat_names != NULL ) {
388 return SLAP_SCHERR_ATTR_INCONSISTENT;
392 return SLAP_SCHERR_ATTR_DUP;
395 static struct aindexrec *air_old;
398 at_dup_error( void *left, void *right )
410 struct aindexrec *air;
412 AttributeType *sat = *rat;
414 if ( sat->sat_oid ) {
415 air = (struct aindexrec *)
416 ch_calloc( 1, sizeof(struct aindexrec) );
417 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
421 if ( avl_insert( &attr_index, (caddr_t) air,
422 attr_index_cmp, at_dup_error ) )
424 AttributeType *old_sat;
429 assert( air_old != NULL );
430 old_sat = air_old->air_at;
432 /* replacing a deleted definition? */
433 if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
436 /* Keep old oid, free new oid;
437 * Keep old ads, free new ads;
438 * Keep new everything else, free old
442 old_sat->sat_oid = tmp.sat_oid;
443 tmp.sat_oid = sat->sat_oid;
444 old_sat->sat_ad = tmp.sat_ad;
445 tmp.sat_ad = sat->sat_ad;
449 at_destroy_one( air );
457 rc = at_check_dup( old_sat, sat );
462 /* FIX: temporal consistency check */
463 at_bvfind( &air->air_name );
466 names = sat->sat_names;
469 air = (struct aindexrec *)
470 ch_calloc( 1, sizeof(struct aindexrec) );
471 ber_str2bv( *names, 0, 0, &air->air_name );
473 if ( avl_insert( &attr_index, (caddr_t) air,
474 attr_index_cmp, avl_dup_error ) )
476 AttributeType *old_sat;
481 old_sat = at_bvfind( &air->air_name );
482 assert( old_sat != NULL );
483 rc = at_check_dup( old_sat, sat );
487 while ( names > sat->sat_names ) {
488 struct aindexrec tmpair;
491 ber_str2bv( *names, 0, 0, &tmpair.air_name );
493 air = (struct aindexrec *)avl_delete( &attr_index,
494 (caddr_t)&tmpair, attr_index_cmp );
495 assert( air != NULL );
499 if ( sat->sat_oid ) {
500 struct aindexrec tmpair;
502 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
504 air = (struct aindexrec *)avl_delete( &attr_index,
505 (caddr_t)&tmpair, attr_index_cmp );
506 assert( air != NULL );
512 /* FIX: temporal consistency check */
513 at_bvfind(&air->air_name);
518 if ( sat->sat_oid ) {
519 slap_ad_undef_promote( sat->sat_oid, sat );
522 names = sat->sat_names;
525 slap_ad_undef_promote( *names, sat );
531 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
533 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
541 LDAPAttributeType *at,
543 AttributeType **rsat,
547 AttributeType *sat = NULL;
548 MatchingRule *mr = NULL;
551 int code = LDAP_SUCCESS;
557 return SLAP_SCHERR_ATTR_INCOMPLETE;
560 if ( !OID_LEADCHAR( at->at_oid[0] )) {
563 /* Expand OID macros */
564 oid = oidm_find( at->at_oid );
567 return SLAP_SCHERR_OIDM;
569 if ( oid != at->at_oid ) {
575 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
578 /* Expand OID macros */
579 oid = oidm_find( at->at_syntax_oid );
581 *err = at->at_syntax_oid;
582 code = SLAP_SCHERR_OIDM;
585 if ( oid != at->at_syntax_oid ) {
586 ldap_memfree( at->at_syntax_oid );
587 at->at_syntax_oid = oid;
591 if ( at->at_names && at->at_names[0] ) {
594 for( i=0; at->at_names[i]; i++ ) {
595 if( !slap_valid_descr( at->at_names[i] ) ) {
596 *err = at->at_names[i];
597 code = SLAP_SCHERR_BAD_DESCR;
602 cname = at->at_names[0];
611 if ( !at->at_usage && at->at_no_user_mod ) {
612 /* user attribute must be modifable */
613 code = SLAP_SCHERR_ATTR_BAD_USAGE;
617 if ( at->at_collective ) {
619 /* collective attributes cannot be operational */
620 code = SLAP_SCHERR_ATTR_BAD_USAGE;
624 if( at->at_single_value ) {
625 /* collective attributes cannot be single-valued */
626 code = SLAP_SCHERR_ATTR_BAD_USAGE;
631 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
632 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
634 sat->sat_cname.bv_val = cname;
635 sat->sat_cname.bv_len = strlen( cname );
636 sat->sat_oidmacro = oidm;
637 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
639 if ( at->at_sup_oid ) {
640 AttributeType *supsat = at_find(at->at_sup_oid);
642 if ( supsat == NULL ) {
643 *err = at->at_sup_oid;
644 code = SLAP_SCHERR_ATTR_NOT_FOUND;
648 sat->sat_sup = supsat;
650 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
651 code = SLAP_SCHERR_OUTOFMEM;
655 if ( sat->sat_usage != supsat->sat_usage ) {
656 /* subtypes must have same usage as their SUP */
657 code = SLAP_SCHERR_ATTR_BAD_USAGE;
661 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
662 /* subtypes must be obsolete if super is */
663 code = SLAP_SCHERR_ATTR_BAD_SUP;
667 if ( sat->sat_flags & SLAP_AT_FINAL ) {
668 /* cannot subtype a "final" attribute type */
669 code = SLAP_SCHERR_ATTR_BAD_SUP;
675 * Inherit definitions from superiors. We only check the
676 * direct superior since that one has already inherited from
679 if ( sat->sat_sup ) {
680 sat->sat_syntax = sat->sat_sup->sat_syntax;
681 sat->sat_equality = sat->sat_sup->sat_equality;
682 sat->sat_approx = sat->sat_sup->sat_approx;
683 sat->sat_ordering = sat->sat_sup->sat_ordering;
684 sat->sat_substr = sat->sat_sup->sat_substr;
688 * check for X-ORDERED attributes
690 if ( sat->sat_extensions ) {
691 for (i=0; sat->sat_extensions[i]; i++) {
692 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
693 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
694 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
696 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
698 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
700 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
708 sat->sat_flags |= SLAP_AT_HARDCODE;
710 if ( at->at_syntax_oid ) {
711 syn = syn_find(sat->sat_syntax_oid);
713 *err = sat->sat_syntax_oid;
714 code = SLAP_SCHERR_SYN_NOT_FOUND;
718 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
719 code = SLAP_SCHERR_ATTR_BAD_SUP;
723 sat->sat_syntax = syn;
725 } else if ( sat->sat_syntax == NULL ) {
726 code = SLAP_SCHERR_ATTR_INCOMPLETE;
730 if ( sat->sat_equality_oid ) {
731 mr = mr_find(sat->sat_equality_oid);
734 *err = sat->sat_equality_oid;
735 code = SLAP_SCHERR_MR_NOT_FOUND;
739 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
740 *err = sat->sat_equality_oid;
741 code = SLAP_SCHERR_ATTR_BAD_MR;
745 if( sat->sat_syntax != mr->smr_syntax ) {
746 if( mr->smr_compat_syntaxes == NULL ) {
747 *err = sat->sat_equality_oid;
748 code = SLAP_SCHERR_ATTR_BAD_MR;
752 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
753 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
760 *err = sat->sat_equality_oid;
761 code = SLAP_SCHERR_ATTR_BAD_MR;
766 sat->sat_equality = mr;
767 sat->sat_approx = mr->smr_associated;
770 if ( sat->sat_ordering_oid ) {
771 if( !sat->sat_equality ) {
772 *err = sat->sat_ordering_oid;
773 code = SLAP_SCHERR_ATTR_BAD_MR;
777 mr = mr_find(sat->sat_ordering_oid);
780 *err = sat->sat_ordering_oid;
781 code = SLAP_SCHERR_MR_NOT_FOUND;
785 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
786 *err = sat->sat_ordering_oid;
787 code = SLAP_SCHERR_ATTR_BAD_MR;
791 if( sat->sat_syntax != mr->smr_syntax ) {
792 if( mr->smr_compat_syntaxes == NULL ) {
793 *err = sat->sat_ordering_oid;
794 code = SLAP_SCHERR_ATTR_BAD_MR;
798 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
799 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
806 *err = sat->sat_ordering_oid;
807 code = SLAP_SCHERR_ATTR_BAD_MR;
812 sat->sat_ordering = mr;
815 if ( sat->sat_substr_oid ) {
816 if( !sat->sat_equality ) {
817 *err = sat->sat_substr_oid;
818 code = SLAP_SCHERR_ATTR_BAD_MR;
822 mr = mr_find(sat->sat_substr_oid);
825 *err = sat->sat_substr_oid;
826 code = SLAP_SCHERR_MR_NOT_FOUND;
830 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
831 *err = sat->sat_substr_oid;
832 code = SLAP_SCHERR_ATTR_BAD_MR;
836 /* due to funky LDAP builtin substring rules,
837 * we check against the equality rule assertion
838 * syntax and compat syntaxes instead of those
839 * associated with the substrings rule.
841 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
842 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
843 *err = sat->sat_substr_oid;
844 code = SLAP_SCHERR_ATTR_BAD_MR;
848 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
849 if( sat->sat_syntax ==
850 sat->sat_equality->smr_compat_syntaxes[i] )
858 *err = sat->sat_substr_oid;
859 code = SLAP_SCHERR_ATTR_BAD_MR;
864 sat->sat_substr = mr;
867 code = at_insert( &sat, prev, err );
871 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
876 SLAP_FREE( at->at_oid );
890 at_index_printnode( void *v_air, void *ignore )
892 struct aindexrec *air = v_air;
894 air->air_name.bv_val,
895 ldap_attributetype2str(&air->air_at->sat_atype) );
900 at_index_print( void )
902 printf("Printing attribute type index:\n");
903 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
909 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
913 struct berval bv, *bva = NULL, idx;
917 start = LDAP_STAILQ_FIRST( &attr_list );
919 /* count the result size */
921 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
922 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
924 if ( at == end ) break;
929 bva = ch_malloc( (num+1) * sizeof(struct berval) );
937 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
938 LDAPAttributeType lat, *latp;
939 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
940 if ( at->sat_oidmacro ) {
942 lat.at_oid = at->sat_oidmacro;
945 latp = &at->sat_atype;
947 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
948 ber_bvarray_free( bva );
951 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
953 bva[i].bv_len = idx.bv_len + bv.bv_len;
954 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
955 strcpy( bva[i].bv_val, ibuf );
956 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
958 bva[i].bv_val = NULL;
959 ldap_memfree( bv.bv_val );
960 if ( at == end ) break;
966 at_schema_info( Entry *e )
968 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
973 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
974 if( at->sat_flags & SLAP_AT_HIDE ) continue;
976 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
980 ber_str2bv( at->sat_oid, 0, 0, &nval );
982 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
986 ldap_memfree( val.bv_val );
992 register_at( char *def, AttributeDescription **rad, int dupok )
994 LDAPAttributeType *at;
995 int code, freeit = 0;
997 AttributeDescription *ad = NULL;
999 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1001 Debug( LDAP_DEBUG_ANY,
1002 "register_at: AttributeType \"%s\": %s, %s\n",
1003 def, ldap_scherr2str(code), err );
1007 code = at_add( at, 0, NULL, NULL, &err );
1009 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1013 ldap_attributetype_free( at );
1014 Debug( LDAP_DEBUG_ANY,
1015 "register_at: AttributeType \"%s\": %s, %s\n",
1016 def, scherr2str(code), err );
1020 code = slap_str2ad( at->at_names[0], &ad, &err );
1021 if ( freeit || code ) {
1022 ldap_attributetype_free( at );
1027 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1030 if ( rad ) *rad = ad;