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);
251 if ( slap_schema.si_at_proxied ) {
252 ad_destroy(slap_schema.si_at_proxied->sat_ad);
257 at_start( AttributeType **at )
259 assert( at != NULL );
261 *at = LDAP_STAILQ_FIRST(&attr_list);
263 return (*at != NULL);
267 at_next( AttributeType **at )
269 assert( at != NULL );
271 #if 1 /* pedantic check */
273 AttributeType *tmp = NULL;
275 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
281 assert( tmp != NULL );
285 *at = LDAP_STAILQ_NEXT(*at,sat_next);
287 return (*at != NULL);
291 * check whether the two attributeTypes actually __are__ identical,
292 * or rather inconsistent
297 AttributeType *new_sat )
299 if ( new_sat->sat_oid != NULL ) {
300 if ( sat->sat_oid == NULL ) {
301 return SLAP_SCHERR_ATTR_INCONSISTENT;
304 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
305 return SLAP_SCHERR_ATTR_INCONSISTENT;
309 if ( sat->sat_oid != NULL ) {
310 return SLAP_SCHERR_ATTR_INCONSISTENT;
314 if ( new_sat->sat_names ) {
317 if ( sat->sat_names == NULL ) {
318 return SLAP_SCHERR_ATTR_INCONSISTENT;
321 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
322 if ( sat->sat_names[ i ] == NULL ) {
323 return SLAP_SCHERR_ATTR_INCONSISTENT;
326 if ( strcasecmp( sat->sat_names[ i ],
327 new_sat->sat_names[ i ] ) != 0 )
329 return SLAP_SCHERR_ATTR_INCONSISTENT;
333 if ( sat->sat_names != NULL ) {
334 return SLAP_SCHERR_ATTR_INCONSISTENT;
338 return SLAP_SCHERR_ATTR_DUP;
347 struct aindexrec *air;
351 if ( sat->sat_oid ) {
352 air = (struct aindexrec *)
353 ch_calloc( 1, sizeof(struct aindexrec) );
354 air->air_name.bv_val = sat->sat_oid;
355 air->air_name.bv_len = strlen(sat->sat_oid);
357 if ( avl_insert( &attr_index, (caddr_t) air,
358 attr_index_cmp, avl_dup_error ) )
360 AttributeType *old_sat;
365 old_sat = at_bvfind( &air->air_name );
366 assert( old_sat != NULL );
367 rc = at_check_dup( old_sat, sat );
373 /* FIX: temporal consistency check */
374 at_bvfind( &air->air_name );
377 names = sat->sat_names;
380 air = (struct aindexrec *)
381 ch_calloc( 1, sizeof(struct aindexrec) );
382 air->air_name.bv_val = *names;
383 air->air_name.bv_len = strlen(*names);
385 if ( avl_insert( &attr_index, (caddr_t) air,
386 attr_index_cmp, avl_dup_error ) )
388 AttributeType *old_sat;
393 old_sat = at_bvfind( &air->air_name );
394 assert( old_sat != NULL );
395 rc = at_check_dup( old_sat, sat );
401 /* FIX: temporal consistency check */
402 at_bvfind(&air->air_name);
407 if ( sat->sat_oid ) {
408 slap_ad_undef_promote( sat->sat_oid, sat );
411 names = sat->sat_names;
414 slap_ad_undef_promote( *names, sat );
419 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
426 LDAPAttributeType *at,
428 AttributeType **rsat,
440 if ( !OID_LEADCHAR( at->at_oid[0] )) {
441 /* Expand OID macros */
442 oid = oidm_find( at->at_oid );
445 return SLAP_SCHERR_OIDM;
447 if ( oid != at->at_oid ) {
453 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
454 /* Expand OID macros */
455 oid = oidm_find( at->at_syntax_oid );
457 *err = at->at_syntax_oid;
458 return SLAP_SCHERR_OIDM;
460 if ( oid != at->at_syntax_oid ) {
461 ldap_memfree( at->at_syntax_oid );
462 at->at_syntax_oid = oid;
466 if ( at->at_names && at->at_names[0] ) {
469 for( i=0; at->at_names[i]; i++ ) {
470 if( !slap_valid_descr( at->at_names[i] ) ) {
471 *err = at->at_names[i];
472 return SLAP_SCHERR_BAD_DESCR;
476 cname = at->at_names[0];
478 } else if ( at->at_oid ) {
483 return SLAP_SCHERR_ATTR_INCOMPLETE;
488 if ( !at->at_usage && at->at_no_user_mod ) {
489 /* user attribute must be modifable */
490 return SLAP_SCHERR_ATTR_BAD_USAGE;
493 if ( at->at_collective ) {
495 /* collective attributes cannot be operational */
496 return SLAP_SCHERR_ATTR_BAD_USAGE;
499 if( at->at_single_value ) {
500 /* collective attributes cannot be single-valued */
501 return SLAP_SCHERR_ATTR_BAD_USAGE;
505 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
506 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
508 sat->sat_cname.bv_val = cname;
509 sat->sat_cname.bv_len = strlen( cname );
510 sat->sat_oidmacro = oidm;
511 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
513 if ( at->at_sup_oid ) {
514 AttributeType *supsat = at_find(at->at_sup_oid);
516 if ( supsat == NULL ) {
517 *err = at->at_sup_oid;
518 code = SLAP_SCHERR_ATTR_NOT_FOUND;
522 sat->sat_sup = supsat;
524 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
525 code = SLAP_SCHERR_OUTOFMEM;
529 if ( sat->sat_usage != supsat->sat_usage ) {
530 /* subtypes must have same usage as their SUP */
531 code = SLAP_SCHERR_ATTR_BAD_USAGE;
535 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
536 /* subtypes must be obsolete if super is */
537 code = SLAP_SCHERR_ATTR_BAD_SUP;
541 if ( sat->sat_flags & SLAP_AT_FINAL ) {
542 /* cannot subtype a "final" attribute type */
543 code = SLAP_SCHERR_ATTR_BAD_SUP;
549 * Inherit definitions from superiors. We only check the
550 * direct superior since that one has already inherited from
553 if ( sat->sat_sup ) {
554 sat->sat_syntax = sat->sat_sup->sat_syntax;
555 sat->sat_equality = sat->sat_sup->sat_equality;
556 sat->sat_approx = sat->sat_sup->sat_approx;
557 sat->sat_ordering = sat->sat_sup->sat_ordering;
558 sat->sat_substr = sat->sat_sup->sat_substr;
562 * check for X-ORDERED attributes
564 if ( sat->sat_extensions ) {
565 for (i=0; sat->sat_extensions[i]; i++) {
566 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
567 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
568 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
570 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
572 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
574 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
582 sat->sat_flags |= SLAP_AT_HARDCODE;
584 if ( at->at_syntax_oid ) {
585 syn = syn_find(sat->sat_syntax_oid);
587 *err = sat->sat_syntax_oid;
588 code = SLAP_SCHERR_SYN_NOT_FOUND;
592 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
593 code = SLAP_SCHERR_ATTR_BAD_SUP;
597 sat->sat_syntax = syn;
599 } else if ( sat->sat_syntax == NULL ) {
600 code = SLAP_SCHERR_ATTR_INCOMPLETE;
604 if ( sat->sat_equality_oid ) {
605 mr = mr_find(sat->sat_equality_oid);
608 *err = sat->sat_equality_oid;
609 code = SLAP_SCHERR_MR_NOT_FOUND;
613 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
614 *err = sat->sat_equality_oid;
615 code = SLAP_SCHERR_ATTR_BAD_MR;
619 if( sat->sat_syntax != mr->smr_syntax ) {
620 if( mr->smr_compat_syntaxes == NULL ) {
621 *err = sat->sat_equality_oid;
622 code = SLAP_SCHERR_ATTR_BAD_MR;
626 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
627 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
634 *err = sat->sat_equality_oid;
635 code = SLAP_SCHERR_ATTR_BAD_MR;
640 sat->sat_equality = mr;
641 sat->sat_approx = mr->smr_associated;
644 if ( sat->sat_ordering_oid ) {
645 if( !sat->sat_equality ) {
646 *err = sat->sat_ordering_oid;
647 code = SLAP_SCHERR_ATTR_BAD_MR;
651 mr = mr_find(sat->sat_ordering_oid);
654 *err = sat->sat_ordering_oid;
655 code = SLAP_SCHERR_MR_NOT_FOUND;
659 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
660 *err = sat->sat_ordering_oid;
661 code = SLAP_SCHERR_ATTR_BAD_MR;
665 if( sat->sat_syntax != mr->smr_syntax ) {
666 if( mr->smr_compat_syntaxes == NULL ) {
667 *err = sat->sat_ordering_oid;
668 code = SLAP_SCHERR_ATTR_BAD_MR;
672 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
673 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
680 *err = sat->sat_ordering_oid;
681 code = SLAP_SCHERR_ATTR_BAD_MR;
686 sat->sat_ordering = mr;
689 if ( sat->sat_substr_oid ) {
690 if( !sat->sat_equality ) {
691 *err = sat->sat_substr_oid;
692 code = SLAP_SCHERR_ATTR_BAD_MR;
696 mr = mr_find(sat->sat_substr_oid);
699 *err = sat->sat_substr_oid;
700 code = SLAP_SCHERR_MR_NOT_FOUND;
704 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
705 *err = sat->sat_substr_oid;
706 code = SLAP_SCHERR_ATTR_BAD_MR;
710 /* due to funky LDAP builtin substring rules,
711 * we check against the equality rule assertion
712 * syntax and compat syntaxes instead of those
713 * associated with the substrings rule.
715 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
716 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
717 *err = sat->sat_substr_oid;
718 code = SLAP_SCHERR_ATTR_BAD_MR;
722 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
723 if( sat->sat_syntax ==
724 sat->sat_equality->smr_compat_syntaxes[i] )
732 *err = sat->sat_substr_oid;
733 code = SLAP_SCHERR_ATTR_BAD_MR;
738 sat->sat_substr = mr;
741 code = at_insert( sat, err );
745 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
750 SLAP_FREE( at->at_oid );
763 at_index_printnode( void *v_air, void *ignore )
765 struct aindexrec *air = v_air;
767 air->air_name.bv_val,
768 ldap_attributetype2str(&air->air_at->sat_atype) );
773 at_index_print( void )
775 printf("Printing attribute type index:\n");
776 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
781 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
785 struct berval bv, *bva = NULL, idx;
789 start = LDAP_STAILQ_FIRST( &attr_list );
791 /* count the result size */
793 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
794 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
796 if ( at == end ) break;
801 bva = ch_malloc( (num+1) * sizeof(struct berval) );
809 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
810 LDAPAttributeType lat, *latp;
811 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
812 if ( at->sat_oidmacro ) {
814 lat.at_oid = at->sat_oidmacro;
817 latp = &at->sat_atype;
819 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
820 ber_bvarray_free( bva );
823 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
825 bva[i].bv_len = idx.bv_len + bv.bv_len;
826 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
827 strcpy( bva[i].bv_val, ibuf );
828 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
830 bva[i].bv_val = NULL;
831 ldap_memfree( bv.bv_val );
832 if ( at == end ) break;
838 at_schema_info( Entry *e )
840 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
845 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
846 if( at->sat_flags & SLAP_AT_HIDE ) continue;
848 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
852 ber_str2bv( at->sat_oid, 0, 0, &nval );
854 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
858 ldap_memfree( val.bv_val );