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_subtypes) ldap_memfree(a->sat_subtypes);
218 ad_destroy(a->sat_ad);
219 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
220 ldap_attributetype_free((LDAPAttributeType *)a);
223 if ( slap_schema.si_at_undefined ) {
224 ad_destroy(slap_schema.si_at_undefined->sat_ad);
229 at_start( AttributeType **at )
231 assert( at != NULL );
233 *at = LDAP_STAILQ_FIRST(&attr_list);
235 return (*at != NULL);
239 at_next( AttributeType **at )
241 assert( at != NULL );
243 #if 1 /* pedantic check */
245 AttributeType *tmp = NULL;
247 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
253 assert( tmp != NULL );
257 *at = LDAP_STAILQ_NEXT(*at,sat_next);
259 return (*at != NULL);
263 * check whether the two attributeTypes actually __are__ identical,
264 * or rather inconsistent
269 AttributeType *new_sat )
271 if ( new_sat->sat_oid != NULL ) {
272 if ( sat->sat_oid == NULL ) {
273 return SLAP_SCHERR_ATTR_INCONSISTENT;
276 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
277 return SLAP_SCHERR_ATTR_INCONSISTENT;
281 if ( sat->sat_oid != NULL ) {
282 return SLAP_SCHERR_ATTR_INCONSISTENT;
286 if ( new_sat->sat_names ) {
289 if ( sat->sat_names == NULL ) {
290 return SLAP_SCHERR_ATTR_INCONSISTENT;
293 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
294 if ( sat->sat_names[ i ] == NULL ) {
295 return SLAP_SCHERR_ATTR_INCONSISTENT;
298 if ( strcasecmp( sat->sat_names[ i ],
299 new_sat->sat_names[ i ] ) != 0 )
301 return SLAP_SCHERR_ATTR_INCONSISTENT;
305 if ( sat->sat_names != NULL ) {
306 return SLAP_SCHERR_ATTR_INCONSISTENT;
310 return SLAP_SCHERR_ATTR_DUP;
319 struct aindexrec *air;
323 if ( sat->sat_oid ) {
324 air = (struct aindexrec *)
325 ch_calloc( 1, sizeof(struct aindexrec) );
326 air->air_name.bv_val = sat->sat_oid;
327 air->air_name.bv_len = strlen(sat->sat_oid);
329 if ( avl_insert( &attr_index, (caddr_t) air,
330 attr_index_cmp, avl_dup_error ) )
332 AttributeType *old_sat;
337 old_sat = at_bvfind( &air->air_name );
338 assert( old_sat != NULL );
339 rc = at_check_dup( old_sat, sat );
345 /* FIX: temporal consistency check */
346 at_bvfind( &air->air_name );
349 names = sat->sat_names;
352 air = (struct aindexrec *)
353 ch_calloc( 1, sizeof(struct aindexrec) );
354 air->air_name.bv_val = *names;
355 air->air_name.bv_len = strlen(*names);
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);
379 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
386 LDAPAttributeType *at,
388 AttributeType **rsat,
400 if ( !OID_LEADCHAR( at->at_oid[0] )) {
401 /* Expand OID macros */
402 oid = oidm_find( at->at_oid );
405 return SLAP_SCHERR_OIDM;
407 if ( oid != at->at_oid ) {
413 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
414 /* Expand OID macros */
415 oid = oidm_find( at->at_syntax_oid );
417 *err = at->at_syntax_oid;
418 return SLAP_SCHERR_OIDM;
420 if ( oid != at->at_syntax_oid ) {
421 ldap_memfree( at->at_syntax_oid );
422 at->at_syntax_oid = oid;
426 if ( at->at_names && at->at_names[0] ) {
429 for( i=0; at->at_names[i]; i++ ) {
430 if( !slap_valid_descr( at->at_names[i] ) ) {
431 *err = at->at_names[i];
432 return SLAP_SCHERR_BAD_DESCR;
436 cname = at->at_names[0];
438 } else if ( at->at_oid ) {
443 return SLAP_SCHERR_ATTR_INCOMPLETE;
448 if ( !at->at_usage && at->at_no_user_mod ) {
449 /* user attribute must be modifable */
450 return SLAP_SCHERR_ATTR_BAD_USAGE;
453 if ( at->at_collective ) {
455 /* collective attributes cannot be operational */
456 return SLAP_SCHERR_ATTR_BAD_USAGE;
459 if( at->at_single_value ) {
460 /* collective attributes cannot be single-valued */
461 return SLAP_SCHERR_ATTR_BAD_USAGE;
465 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
466 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
468 sat->sat_cname.bv_val = cname;
469 sat->sat_cname.bv_len = strlen( cname );
470 sat->sat_oidmacro = oidm;
471 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
473 if ( at->at_sup_oid ) {
474 AttributeType *supsat = at_find(at->at_sup_oid);
476 if ( supsat == NULL ) {
477 *err = at->at_sup_oid;
478 code = SLAP_SCHERR_ATTR_NOT_FOUND;
482 sat->sat_sup = supsat;
484 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
485 code = SLAP_SCHERR_OUTOFMEM;
489 if ( sat->sat_usage != supsat->sat_usage ) {
490 /* subtypes must have same usage as their SUP */
491 code = SLAP_SCHERR_ATTR_BAD_USAGE;
495 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
496 /* subtypes must be obsolete if super is */
497 code = SLAP_SCHERR_ATTR_BAD_SUP;
501 if ( sat->sat_flags & SLAP_AT_FINAL ) {
502 /* cannot subtype a "final" attribute type */
503 code = SLAP_SCHERR_ATTR_BAD_SUP;
509 * Inherit definitions from superiors. We only check the
510 * direct superior since that one has already inherited from
513 if ( sat->sat_sup ) {
514 sat->sat_syntax = sat->sat_sup->sat_syntax;
515 sat->sat_equality = sat->sat_sup->sat_equality;
516 sat->sat_approx = sat->sat_sup->sat_approx;
517 sat->sat_ordering = sat->sat_sup->sat_ordering;
518 sat->sat_substr = sat->sat_sup->sat_substr;
522 * check for X-ORDERED attributes
524 if ( sat->sat_extensions ) {
525 for (i=0; sat->sat_extensions[i]; i++) {
526 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
527 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
528 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
530 sat->sat_flags |= SLAP_AT_ORDERED_VAL;
532 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
534 sat->sat_flags |= SLAP_AT_ORDERED_SIB;
542 sat->sat_flags |= SLAP_AT_HARDCODE;
544 if ( at->at_syntax_oid ) {
545 syn = syn_find(sat->sat_syntax_oid);
547 *err = sat->sat_syntax_oid;
548 code = SLAP_SCHERR_SYN_NOT_FOUND;
552 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
553 code = SLAP_SCHERR_ATTR_BAD_SUP;
557 sat->sat_syntax = syn;
559 } else if ( sat->sat_syntax == NULL ) {
560 code = SLAP_SCHERR_ATTR_INCOMPLETE;
564 if ( sat->sat_equality_oid ) {
565 mr = mr_find(sat->sat_equality_oid);
568 *err = sat->sat_equality_oid;
569 code = SLAP_SCHERR_MR_NOT_FOUND;
573 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
574 *err = sat->sat_equality_oid;
575 code = SLAP_SCHERR_ATTR_BAD_MR;
579 if( sat->sat_syntax != mr->smr_syntax ) {
580 if( mr->smr_compat_syntaxes == NULL ) {
581 *err = sat->sat_equality_oid;
582 code = SLAP_SCHERR_ATTR_BAD_MR;
586 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
587 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
594 *err = sat->sat_equality_oid;
595 code = SLAP_SCHERR_ATTR_BAD_MR;
600 sat->sat_equality = mr;
601 sat->sat_approx = mr->smr_associated;
604 if ( sat->sat_ordering_oid ) {
605 if( !sat->sat_equality ) {
606 *err = sat->sat_ordering_oid;
607 code = SLAP_SCHERR_ATTR_BAD_MR;
611 mr = mr_find(sat->sat_ordering_oid);
614 *err = sat->sat_ordering_oid;
615 code = SLAP_SCHERR_MR_NOT_FOUND;
619 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
620 *err = sat->sat_ordering_oid;
621 code = SLAP_SCHERR_ATTR_BAD_MR;
625 if( sat->sat_syntax != mr->smr_syntax ) {
626 if( mr->smr_compat_syntaxes == NULL ) {
627 *err = sat->sat_ordering_oid;
628 code = SLAP_SCHERR_ATTR_BAD_MR;
632 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
633 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
640 *err = sat->sat_ordering_oid;
641 code = SLAP_SCHERR_ATTR_BAD_MR;
646 sat->sat_ordering = mr;
649 if ( sat->sat_substr_oid ) {
650 if( !sat->sat_equality ) {
651 *err = sat->sat_substr_oid;
652 code = SLAP_SCHERR_ATTR_BAD_MR;
656 mr = mr_find(sat->sat_substr_oid);
659 *err = sat->sat_substr_oid;
660 code = SLAP_SCHERR_MR_NOT_FOUND;
664 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
665 *err = sat->sat_substr_oid;
666 code = SLAP_SCHERR_ATTR_BAD_MR;
670 /* due to funky LDAP builtin substring rules,
671 * we check against the equality rule assertion
672 * syntax and compat syntaxes instead of those
673 * associated with the substrings rule.
675 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
676 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
677 *err = sat->sat_substr_oid;
678 code = SLAP_SCHERR_ATTR_BAD_MR;
682 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
683 if( sat->sat_syntax ==
684 sat->sat_equality->smr_compat_syntaxes[i] )
692 *err = sat->sat_substr_oid;
693 code = SLAP_SCHERR_ATTR_BAD_MR;
698 sat->sat_substr = mr;
701 code = at_insert( sat, err );
705 ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
718 at_index_printnode( void *v_air, void *ignore )
720 struct aindexrec *air = v_air;
722 air->air_name.bv_val,
723 ldap_attributetype2str(&air->air_at->sat_atype) );
728 at_index_print( void )
730 printf("Printing attribute type index:\n");
731 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
736 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
740 struct berval bv, *bva = NULL, idx;
744 start = LDAP_STAILQ_FIRST( &attr_list );
746 /* count the result size */
748 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
749 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
751 if ( at == end ) break;
756 bva = ch_malloc( (num+1) * sizeof(struct berval) );
764 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
765 LDAPAttributeType lat, *latp;
766 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
767 if ( at->sat_oidmacro ) {
769 lat.at_oid = at->sat_oidmacro;
772 latp = &at->sat_atype;
774 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
775 ber_bvarray_free( bva );
778 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
780 bva[i].bv_len = idx.bv_len + bv.bv_len;
781 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
782 strcpy( bva[i].bv_val, ibuf );
783 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
785 bva[i].bv_val = NULL;
786 ldap_memfree( bv.bv_val );
787 if ( at == end ) break;
793 at_schema_info( Entry *e )
795 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
800 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
801 if( at->sat_flags & SLAP_AT_HIDE ) continue;
803 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
807 ber_str2bv( at->sat_oid, 0, 0, &nval );
809 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
813 ldap_memfree( val.bv_val );