1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 );
113 if ( air && ( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
114 avl_insert( &attr_cache, (caddr_t) air,
115 attr_index_cmp, avl_dup_error );
118 return air != NULL ? air->air_at : NULL;
124 AttributeType ***listp )
126 AttributeType **list;
127 AttributeType **list1;
133 list = ch_calloc(size, sizeof(AttributeType *));
145 list1 = ch_realloc(list, size*sizeof(AttributeType *));
160 AttributeType ***listp )
162 AttributeType **list;
163 AttributeType **list1;
171 for ( i=0; list[i]; i++ )
176 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
180 /* Tell the runtime this can be shrinked */
181 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
192 AttributeType **list )
199 for ( i=0; list[i]; i++ ) {
200 if ( sat == list[i] ) {
211 avl_free(attr_index, ldap_memfree);
213 while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
214 a = LDAP_STAILQ_FIRST(&attr_list);
215 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
217 if ( a->sat_equality ) {
220 mr = mr_find( a->sat_equality->smr_oid );
221 assert( mr != NULL );
222 if ( mr != a->sat_equality ) {
223 ch_free( a->sat_equality );
224 a->sat_equality = NULL;
228 assert( a->sat_syntax != NULL );
229 if ( a->sat_syntax != NULL ) {
232 syn = syn_find( a->sat_syntax->ssyn_oid );
233 assert( syn != NULL );
234 if ( syn != a->sat_syntax ) {
235 ch_free( a->sat_syntax );
236 a->sat_syntax = NULL;
240 if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
241 if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
242 ad_destroy(a->sat_ad);
243 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
244 ldap_attributetype_free((LDAPAttributeType *)a);
247 if ( slap_schema.si_at_undefined ) {
248 ad_destroy(slap_schema.si_at_undefined->sat_ad);
253 at_start( AttributeType **at )
255 assert( at != NULL );
257 *at = LDAP_STAILQ_FIRST(&attr_list);
259 return (*at != NULL);
263 at_next( AttributeType **at )
265 assert( at != NULL );
267 #if 1 /* pedantic check */
269 AttributeType *tmp = NULL;
271 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
277 assert( tmp != NULL );
281 *at = LDAP_STAILQ_NEXT(*at,sat_next);
283 return (*at != NULL);
287 * check whether the two attributeTypes actually __are__ identical,
288 * or rather inconsistent
293 AttributeType *new_sat )
295 if ( new_sat->sat_oid != NULL ) {
296 if ( sat->sat_oid == NULL ) {
297 return SLAP_SCHERR_ATTR_INCONSISTENT;
300 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
301 return SLAP_SCHERR_ATTR_INCONSISTENT;
305 if ( sat->sat_oid != NULL ) {
306 return SLAP_SCHERR_ATTR_INCONSISTENT;
310 if ( new_sat->sat_names ) {
313 if ( sat->sat_names == NULL ) {
314 return SLAP_SCHERR_ATTR_INCONSISTENT;
317 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
318 if ( sat->sat_names[ i ] == NULL ) {
319 return SLAP_SCHERR_ATTR_INCONSISTENT;
322 if ( strcasecmp( sat->sat_names[ i ],
323 new_sat->sat_names[ i ] ) != 0 )
325 return SLAP_SCHERR_ATTR_INCONSISTENT;
329 if ( sat->sat_names != NULL ) {
330 return SLAP_SCHERR_ATTR_INCONSISTENT;
334 return SLAP_SCHERR_ATTR_DUP;
343 struct aindexrec *air;
347 if ( sat->sat_oid ) {
348 air = (struct aindexrec *)
349 ch_calloc( 1, sizeof(struct aindexrec) );
350 air->air_name.bv_val = sat->sat_oid;
351 air->air_name.bv_len = strlen(sat->sat_oid);
353 if ( avl_insert( &attr_index, (caddr_t) air,
354 attr_index_cmp, avl_dup_error ) )
356 AttributeType *old_sat;
361 old_sat = at_bvfind( &air->air_name );
362 assert( old_sat != NULL );
363 rc = at_check_dup( old_sat, sat );
369 /* FIX: temporal consistency check */
370 at_bvfind( &air->air_name );
373 names = sat->sat_names;
376 air = (struct aindexrec *)
377 ch_calloc( 1, sizeof(struct aindexrec) );
378 air->air_name.bv_val = *names;
379 air->air_name.bv_len = strlen(*names);
381 if ( avl_insert( &attr_index, (caddr_t) air,
382 attr_index_cmp, avl_dup_error ) )
384 AttributeType *old_sat;
389 old_sat = at_bvfind( &air->air_name );
390 assert( old_sat != NULL );
391 rc = at_check_dup( old_sat, sat );
397 /* FIX: temporal consistency check */
398 at_bvfind(&air->air_name);
403 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
410 LDAPAttributeType *at,
412 AttributeType **rsat,
424 if ( !OID_LEADCHAR( at->at_oid[0] )) {
425 /* Expand OID macros */
426 oid = oidm_find( at->at_oid );
429 return SLAP_SCHERR_OIDM;
431 if ( oid != at->at_oid ) {
437 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
438 /* Expand OID macros */
439 oid = oidm_find( at->at_syntax_oid );
441 *err = at->at_syntax_oid;
442 return SLAP_SCHERR_OIDM;
444 if ( oid != at->at_syntax_oid ) {
445 ldap_memfree( at->at_syntax_oid );
446 at->at_syntax_oid = oid;
450 if ( at->at_names && at->at_names[0] ) {
453 for( i=0; at->at_names[i]; i++ ) {
454 if( !slap_valid_descr( at->at_names[i] ) ) {
455 *err = at->at_names[i];
456 return SLAP_SCHERR_BAD_DESCR;
460 cname = at->at_names[0];
462 } else if ( at->at_oid ) {
467 return SLAP_SCHERR_ATTR_INCOMPLETE;
472 if ( !at->at_usage && at->at_no_user_mod ) {
473 /* user attribute must be modifable */
474 return SLAP_SCHERR_ATTR_BAD_USAGE;
477 if ( at->at_collective ) {
479 /* collective attributes cannot be operational */
480 return SLAP_SCHERR_ATTR_BAD_USAGE;
483 if( at->at_single_value ) {
484 /* collective attributes cannot be single-valued */
485 return SLAP_SCHERR_ATTR_BAD_USAGE;
489 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
490 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
492 sat->sat_cname.bv_val = cname;
493 sat->sat_cname.bv_len = strlen( cname );
494 sat->sat_oidmacro = oidm;
495 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
497 if ( at->at_sup_oid ) {
498 AttributeType *supsat = at_find(at->at_sup_oid);
500 if ( supsat == NULL ) {
501 *err = at->at_sup_oid;
502 code = SLAP_SCHERR_ATTR_NOT_FOUND;
506 sat->sat_sup = supsat;
508 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
509 code = SLAP_SCHERR_OUTOFMEM;
513 if ( sat->sat_usage != supsat->sat_usage ) {
514 /* subtypes must have same usage as their SUP */
515 code = SLAP_SCHERR_ATTR_BAD_USAGE;
519 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
520 /* subtypes must be obsolete if super is */
521 code = SLAP_SCHERR_ATTR_BAD_SUP;
525 if ( sat->sat_flags & SLAP_AT_FINAL ) {
526 /* cannot subtype a "final" attribute type */
527 code = SLAP_SCHERR_ATTR_BAD_SUP;
533 * Inherit definitions from superiors. We only check the
534 * direct superior since that one has already inherited from
537 if ( sat->sat_sup ) {
538 sat->sat_syntax = sat->sat_sup->sat_syntax;
539 sat->sat_equality = sat->sat_sup->sat_equality;
540 sat->sat_approx = sat->sat_sup->sat_approx;
541 sat->sat_ordering = sat->sat_sup->sat_ordering;
542 sat->sat_substr = sat->sat_sup->sat_substr;
546 * check for X-ORDERED attributes
548 if ( sat->sat_extensions ) {
549 for (i=0; sat->sat_extensions[i]; i++) {
550 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
551 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
552 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
554 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
556 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
558 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
566 sat->sat_flags |= SLAP_AT_HARDCODE;
568 if ( at->at_syntax_oid ) {
569 syn = syn_find(sat->sat_syntax_oid);
571 *err = sat->sat_syntax_oid;
572 code = SLAP_SCHERR_SYN_NOT_FOUND;
576 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
577 code = SLAP_SCHERR_ATTR_BAD_SUP;
581 sat->sat_syntax = syn;
583 } else if ( sat->sat_syntax == NULL ) {
584 code = SLAP_SCHERR_ATTR_INCOMPLETE;
588 if ( sat->sat_equality_oid ) {
589 mr = mr_find(sat->sat_equality_oid);
592 *err = sat->sat_equality_oid;
593 code = SLAP_SCHERR_MR_NOT_FOUND;
597 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
598 *err = sat->sat_equality_oid;
599 code = SLAP_SCHERR_ATTR_BAD_MR;
603 if( sat->sat_syntax != mr->smr_syntax ) {
604 if( mr->smr_compat_syntaxes == NULL ) {
605 *err = sat->sat_equality_oid;
606 code = SLAP_SCHERR_ATTR_BAD_MR;
610 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
611 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
618 *err = sat->sat_equality_oid;
619 code = SLAP_SCHERR_ATTR_BAD_MR;
624 sat->sat_equality = mr;
625 sat->sat_approx = mr->smr_associated;
628 if ( sat->sat_ordering_oid ) {
629 if( !sat->sat_equality ) {
630 *err = sat->sat_ordering_oid;
631 code = SLAP_SCHERR_ATTR_BAD_MR;
635 mr = mr_find(sat->sat_ordering_oid);
638 *err = sat->sat_ordering_oid;
639 code = SLAP_SCHERR_MR_NOT_FOUND;
643 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
644 *err = sat->sat_ordering_oid;
645 code = SLAP_SCHERR_ATTR_BAD_MR;
649 if( sat->sat_syntax != mr->smr_syntax ) {
650 if( mr->smr_compat_syntaxes == NULL ) {
651 *err = sat->sat_ordering_oid;
652 code = SLAP_SCHERR_ATTR_BAD_MR;
656 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
657 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
664 *err = sat->sat_ordering_oid;
665 code = SLAP_SCHERR_ATTR_BAD_MR;
670 sat->sat_ordering = mr;
673 if ( sat->sat_substr_oid ) {
674 if( !sat->sat_equality ) {
675 *err = sat->sat_substr_oid;
676 code = SLAP_SCHERR_ATTR_BAD_MR;
680 mr = mr_find(sat->sat_substr_oid);
683 *err = sat->sat_substr_oid;
684 code = SLAP_SCHERR_MR_NOT_FOUND;
688 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
689 *err = sat->sat_substr_oid;
690 code = SLAP_SCHERR_ATTR_BAD_MR;
694 /* due to funky LDAP builtin substring rules,
695 * we check against the equality rule assertion
696 * syntax and compat syntaxes instead of those
697 * associated with the substrings rule.
699 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
700 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
701 *err = sat->sat_substr_oid;
702 code = SLAP_SCHERR_ATTR_BAD_MR;
706 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
707 if( sat->sat_syntax ==
708 sat->sat_equality->smr_compat_syntaxes[i] )
716 *err = sat->sat_substr_oid;
717 code = SLAP_SCHERR_ATTR_BAD_MR;
722 sat->sat_substr = mr;
725 code = at_insert( sat, err );
729 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
742 at_index_printnode( void *v_air, void *ignore )
744 struct aindexrec *air = v_air;
746 air->air_name.bv_val,
747 ldap_attributetype2str(&air->air_at->sat_atype) );
752 at_index_print( void )
754 printf("Printing attribute type index:\n");
755 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
760 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
764 struct berval bv, *bva = NULL, idx;
768 start = LDAP_STAILQ_FIRST( &attr_list );
770 /* count the result size */
772 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
773 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
775 if ( at == end ) break;
780 bva = ch_malloc( (num+1) * sizeof(struct berval) );
788 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
789 LDAPAttributeType lat, *latp;
790 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
791 if ( at->sat_oidmacro ) {
793 lat.at_oid = at->sat_oidmacro;
796 latp = &at->sat_atype;
798 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
799 ber_bvarray_free( bva );
802 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
804 bva[i].bv_len = idx.bv_len + bv.bv_len;
805 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
806 strcpy( bva[i].bv_val, ibuf );
807 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
809 bva[i].bv_val = NULL;
810 ldap_memfree( bv.bv_val );
811 if ( at == end ) break;
817 at_schema_info( Entry *e )
819 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
824 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
825 if( at->sat_flags & SLAP_AT_HIDE ) continue;
827 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
831 ber_str2bv( at->sat_oid, 0, 0, &nval );
833 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
837 ldap_memfree( val.bv_val );