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 );
243 at_clean( AttributeType *a )
245 if ( a->sat_equality ) {
248 mr = mr_find( a->sat_equality->smr_oid );
249 assert( mr != NULL );
250 if ( mr != a->sat_equality ) {
251 ch_free( a->sat_equality );
252 a->sat_equality = NULL;
256 assert( a->sat_syntax != NULL );
257 if ( a->sat_syntax != NULL ) {
260 syn = syn_find( a->sat_syntax->ssyn_oid );
261 assert( syn != NULL );
262 if ( syn != a->sat_syntax ) {
263 ch_free( a->sat_syntax );
264 a->sat_syntax = NULL;
268 if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
269 if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
273 at_destroy_one( void *v )
275 struct aindexrec *air = v;
276 AttributeType *a = air->air_at;
279 ad_destroy(a->sat_ad);
280 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
281 ldap_attributetype_free((LDAPAttributeType *)a);
290 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
291 a = LDAP_STAILQ_FIRST(&attr_list);
292 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
294 at_delete_names( a );
297 avl_free(attr_index, at_destroy_one);
299 if ( slap_schema.si_at_undefined ) {
300 ad_destroy(slap_schema.si_at_undefined->sat_ad);
303 if ( slap_schema.si_at_proxied ) {
304 ad_destroy(slap_schema.si_at_proxied->sat_ad);
309 at_start( AttributeType **at )
311 assert( at != NULL );
313 *at = LDAP_STAILQ_FIRST(&attr_list);
315 return (*at != NULL);
319 at_next( AttributeType **at )
321 assert( at != NULL );
323 #if 1 /* pedantic check */
325 AttributeType *tmp = NULL;
327 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
333 assert( tmp != NULL );
337 *at = LDAP_STAILQ_NEXT(*at,sat_next);
339 return (*at != NULL);
343 * check whether the two attributeTypes actually __are__ identical,
344 * or rather inconsistent
349 AttributeType *new_sat )
351 if ( new_sat->sat_oid != NULL ) {
352 if ( sat->sat_oid == NULL ) {
353 return SLAP_SCHERR_ATTR_INCONSISTENT;
356 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
357 return SLAP_SCHERR_ATTR_INCONSISTENT;
361 if ( sat->sat_oid != NULL ) {
362 return SLAP_SCHERR_ATTR_INCONSISTENT;
366 if ( new_sat->sat_names ) {
369 if ( sat->sat_names == NULL ) {
370 return SLAP_SCHERR_ATTR_INCONSISTENT;
373 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
374 if ( sat->sat_names[ i ] == NULL ) {
375 return SLAP_SCHERR_ATTR_INCONSISTENT;
378 if ( strcasecmp( sat->sat_names[ i ],
379 new_sat->sat_names[ i ] ) != 0 )
381 return SLAP_SCHERR_ATTR_INCONSISTENT;
385 if ( sat->sat_names != NULL ) {
386 return SLAP_SCHERR_ATTR_INCONSISTENT;
390 return SLAP_SCHERR_ATTR_DUP;
393 static struct aindexrec *air_old;
396 at_dup_error( void *left, void *right )
408 struct aindexrec *air;
410 AttributeType *sat = *rat;
412 if ( sat->sat_oid ) {
413 air = (struct aindexrec *)
414 ch_calloc( 1, sizeof(struct aindexrec) );
415 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
419 if ( avl_insert( &attr_index, (caddr_t) air,
420 attr_index_cmp, at_dup_error ) )
422 AttributeType *old_sat;
427 assert( air_old != NULL );
428 old_sat = air_old->air_at;
430 /* replacing a deleted definition? */
431 if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
434 /* Keep old oid, free new oid;
435 * Keep old ads, free new ads;
436 * Keep new everything else, free old
440 old_sat->sat_oid = tmp.sat_oid;
441 tmp.sat_oid = sat->sat_oid;
442 old_sat->sat_ad = tmp.sat_ad;
443 tmp.sat_ad = sat->sat_ad;
447 at_destroy_one( air );
455 rc = at_check_dup( old_sat, sat );
460 /* FIX: temporal consistency check */
461 at_bvfind( &air->air_name );
464 names = sat->sat_names;
467 air = (struct aindexrec *)
468 ch_calloc( 1, sizeof(struct aindexrec) );
469 ber_str2bv( *names, 0, 0, &air->air_name );
471 if ( avl_insert( &attr_index, (caddr_t) air,
472 attr_index_cmp, avl_dup_error ) )
474 AttributeType *old_sat;
479 old_sat = at_bvfind( &air->air_name );
480 assert( old_sat != NULL );
481 rc = at_check_dup( old_sat, sat );
485 while ( names > sat->sat_names ) {
486 struct aindexrec tmpair;
489 ber_str2bv( *names, 0, 0, &tmpair.air_name );
491 air = (struct aindexrec *)avl_delete( &attr_index,
492 (caddr_t)&tmpair, attr_index_cmp );
493 assert( air != NULL );
497 if ( sat->sat_oid ) {
498 struct aindexrec tmpair;
500 ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
502 air = (struct aindexrec *)avl_delete( &attr_index,
503 (caddr_t)&tmpair, attr_index_cmp );
504 assert( air != NULL );
510 /* FIX: temporal consistency check */
511 at_bvfind(&air->air_name);
516 if ( sat->sat_oid ) {
517 slap_ad_undef_promote( sat->sat_oid, sat );
520 names = sat->sat_names;
523 slap_ad_undef_promote( *names, sat );
529 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
531 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
539 LDAPAttributeType *at,
541 AttributeType **rsat,
545 AttributeType *sat = NULL;
546 MatchingRule *mr = NULL;
549 int code = LDAP_SUCCESS;
555 return SLAP_SCHERR_ATTR_INCOMPLETE;
558 if ( !OID_LEADCHAR( at->at_oid[0] )) {
561 /* Expand OID macros */
562 oid = oidm_find( at->at_oid );
565 return SLAP_SCHERR_OIDM;
567 if ( oid != at->at_oid ) {
573 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
576 /* Expand OID macros */
577 oid = oidm_find( at->at_syntax_oid );
579 *err = at->at_syntax_oid;
580 code = SLAP_SCHERR_OIDM;
583 if ( oid != at->at_syntax_oid ) {
584 ldap_memfree( at->at_syntax_oid );
585 at->at_syntax_oid = oid;
589 if ( at->at_names && at->at_names[0] ) {
592 for( i=0; at->at_names[i]; i++ ) {
593 if( !slap_valid_descr( at->at_names[i] ) ) {
594 *err = at->at_names[i];
595 code = SLAP_SCHERR_BAD_DESCR;
600 cname = at->at_names[0];
609 if ( !at->at_usage && at->at_no_user_mod ) {
610 /* user attribute must be modifable */
611 code = SLAP_SCHERR_ATTR_BAD_USAGE;
615 if ( at->at_collective ) {
617 /* collective attributes cannot be operational */
618 code = SLAP_SCHERR_ATTR_BAD_USAGE;
622 if( at->at_single_value ) {
623 /* collective attributes cannot be single-valued */
624 code = SLAP_SCHERR_ATTR_BAD_USAGE;
629 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
630 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
632 sat->sat_cname.bv_val = cname;
633 sat->sat_cname.bv_len = strlen( cname );
634 sat->sat_oidmacro = oidm;
635 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
637 if ( at->at_sup_oid ) {
638 AttributeType *supsat = at_find(at->at_sup_oid);
640 if ( supsat == NULL ) {
641 *err = at->at_sup_oid;
642 code = SLAP_SCHERR_ATTR_NOT_FOUND;
646 sat->sat_sup = supsat;
648 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
649 code = SLAP_SCHERR_OUTOFMEM;
653 if ( sat->sat_usage != supsat->sat_usage ) {
654 /* subtypes must have same usage as their SUP */
655 code = SLAP_SCHERR_ATTR_BAD_USAGE;
659 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
660 /* subtypes must be obsolete if super is */
661 code = SLAP_SCHERR_ATTR_BAD_SUP;
665 if ( sat->sat_flags & SLAP_AT_FINAL ) {
666 /* cannot subtype a "final" attribute type */
667 code = SLAP_SCHERR_ATTR_BAD_SUP;
673 * Inherit definitions from superiors. We only check the
674 * direct superior since that one has already inherited from
677 if ( sat->sat_sup ) {
678 sat->sat_syntax = sat->sat_sup->sat_syntax;
679 sat->sat_equality = sat->sat_sup->sat_equality;
680 sat->sat_approx = sat->sat_sup->sat_approx;
681 sat->sat_ordering = sat->sat_sup->sat_ordering;
682 sat->sat_substr = sat->sat_sup->sat_substr;
686 * check for X-ORDERED attributes
688 if ( sat->sat_extensions ) {
689 for (i=0; sat->sat_extensions[i]; i++) {
690 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
691 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
692 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
694 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
696 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
698 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
706 sat->sat_flags |= SLAP_AT_HARDCODE;
708 if ( at->at_syntax_oid ) {
709 syn = syn_find(sat->sat_syntax_oid);
711 *err = sat->sat_syntax_oid;
712 code = SLAP_SCHERR_SYN_NOT_FOUND;
716 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
717 code = SLAP_SCHERR_ATTR_BAD_SUP;
721 sat->sat_syntax = syn;
723 } else if ( sat->sat_syntax == NULL ) {
724 code = SLAP_SCHERR_ATTR_INCOMPLETE;
728 if ( sat->sat_equality_oid ) {
729 mr = mr_find(sat->sat_equality_oid);
732 *err = sat->sat_equality_oid;
733 code = SLAP_SCHERR_MR_NOT_FOUND;
737 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
738 *err = sat->sat_equality_oid;
739 code = SLAP_SCHERR_ATTR_BAD_MR;
743 if( sat->sat_syntax != mr->smr_syntax ) {
744 if( mr->smr_compat_syntaxes == NULL ) {
745 *err = sat->sat_equality_oid;
746 code = SLAP_SCHERR_ATTR_BAD_MR;
750 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
751 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
758 *err = sat->sat_equality_oid;
759 code = SLAP_SCHERR_ATTR_BAD_MR;
764 sat->sat_equality = mr;
765 sat->sat_approx = mr->smr_associated;
768 if ( sat->sat_ordering_oid ) {
769 if( !sat->sat_equality ) {
770 *err = sat->sat_ordering_oid;
771 code = SLAP_SCHERR_ATTR_BAD_MR;
775 mr = mr_find(sat->sat_ordering_oid);
778 *err = sat->sat_ordering_oid;
779 code = SLAP_SCHERR_MR_NOT_FOUND;
783 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
784 *err = sat->sat_ordering_oid;
785 code = SLAP_SCHERR_ATTR_BAD_MR;
789 if( sat->sat_syntax != mr->smr_syntax ) {
790 if( mr->smr_compat_syntaxes == NULL ) {
791 *err = sat->sat_ordering_oid;
792 code = SLAP_SCHERR_ATTR_BAD_MR;
796 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
797 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
804 *err = sat->sat_ordering_oid;
805 code = SLAP_SCHERR_ATTR_BAD_MR;
810 sat->sat_ordering = mr;
813 if ( sat->sat_substr_oid ) {
814 if( !sat->sat_equality ) {
815 *err = sat->sat_substr_oid;
816 code = SLAP_SCHERR_ATTR_BAD_MR;
820 mr = mr_find(sat->sat_substr_oid);
823 *err = sat->sat_substr_oid;
824 code = SLAP_SCHERR_MR_NOT_FOUND;
828 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
829 *err = sat->sat_substr_oid;
830 code = SLAP_SCHERR_ATTR_BAD_MR;
834 /* due to funky LDAP builtin substring rules,
835 * we check against the equality rule assertion
836 * syntax and compat syntaxes instead of those
837 * associated with the substrings rule.
839 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
840 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
841 *err = sat->sat_substr_oid;
842 code = SLAP_SCHERR_ATTR_BAD_MR;
846 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
847 if( sat->sat_syntax ==
848 sat->sat_equality->smr_compat_syntaxes[i] )
856 *err = sat->sat_substr_oid;
857 code = SLAP_SCHERR_ATTR_BAD_MR;
862 sat->sat_substr = mr;
865 code = at_insert( &sat, prev, err );
869 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
874 SLAP_FREE( at->at_oid );
888 at_index_printnode( void *v_air, void *ignore )
890 struct aindexrec *air = v_air;
892 air->air_name.bv_val,
893 ldap_attributetype2str(&air->air_at->sat_atype) );
898 at_index_print( void )
900 printf("Printing attribute type index:\n");
901 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
907 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
911 struct berval bv, *bva = NULL, idx;
915 start = LDAP_STAILQ_FIRST( &attr_list );
917 /* count the result size */
919 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
920 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
922 if ( at == end ) break;
927 bva = ch_malloc( (num+1) * sizeof(struct berval) );
935 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
936 LDAPAttributeType lat, *latp;
937 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
938 if ( at->sat_oidmacro ) {
940 lat.at_oid = at->sat_oidmacro;
943 latp = &at->sat_atype;
945 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
946 ber_bvarray_free( bva );
949 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
951 bva[i].bv_len = idx.bv_len + bv.bv_len;
952 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
953 strcpy( bva[i].bv_val, ibuf );
954 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
956 bva[i].bv_val = NULL;
957 ldap_memfree( bv.bv_val );
958 if ( at == end ) break;
964 at_schema_info( Entry *e )
966 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
971 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
972 if( at->sat_flags & SLAP_AT_HIDE ) continue;
974 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
978 ber_str2bv( at->sat_oid, 0, 0, &nval );
980 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
984 ldap_memfree( val.bv_val );
990 register_at( char *def, AttributeDescription **rad, int dupok )
992 LDAPAttributeType *at;
993 int code, freeit = 0;
995 AttributeDescription *ad = NULL;
997 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
999 Debug( LDAP_DEBUG_ANY,
1000 "register_at: AttributeType \"%s\": %s, %s\n",
1001 def, ldap_scherr2str(code), err );
1005 code = at_add( at, 0, NULL, NULL, &err );
1007 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1011 ldap_attributetype_free( at );
1012 Debug( LDAP_DEBUG_ANY,
1013 "register_at: AttributeType \"%s\": %s, %s\n",
1014 def, scherr2str(code), err );
1018 code = slap_str2ad( at->at_names[0], &ad, &err );
1019 if ( freeit || code ) {
1020 ldap_attributetype_free( at );
1025 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1028 if ( rad ) *rad = ad;