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 )
233 *at = LDAP_STAILQ_FIRST(&attr_list);
235 return (*at != NULL);
239 at_next( AttributeType **at )
243 #if 1 /* pedantic check */
245 AttributeType *tmp = NULL;
247 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
257 *at = LDAP_STAILQ_NEXT(*at,sat_next);
259 return (*at != NULL);
269 struct aindexrec *air;
272 if ( sat->sat_oid ) {
273 air = (struct aindexrec *)
274 ch_calloc( 1, sizeof(struct aindexrec) );
275 air->air_name.bv_val = sat->sat_oid;
276 air->air_name.bv_len = strlen(sat->sat_oid);
278 if ( avl_insert( &attr_index, (caddr_t) air,
279 attr_index_cmp, avl_dup_error ) ) {
282 return SLAP_SCHERR_ATTR_DUP;
284 /* FIX: temporal consistency check */
285 at_bvfind(&air->air_name);
288 if ( (names = sat->sat_names) ) {
290 air = (struct aindexrec *)
291 ch_calloc( 1, sizeof(struct aindexrec) );
292 air->air_name.bv_val = *names;
293 air->air_name.bv_len = strlen(*names);
295 if ( avl_insert( &attr_index, (caddr_t) air,
296 attr_index_cmp, avl_dup_error ) ) {
299 return SLAP_SCHERR_ATTR_DUP;
301 /* FIX: temporal consistency check */
302 at_bvfind(&air->air_name);
307 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
314 LDAPAttributeType *at,
316 AttributeType **rsat,
327 if ( !OID_LEADCHAR( at->at_oid[0] )) {
328 /* Expand OID macros */
329 oid = oidm_find( at->at_oid );
332 return SLAP_SCHERR_OIDM;
334 if ( oid != at->at_oid ) {
335 ldap_memfree( at->at_oid );
340 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
341 /* Expand OID macros */
342 oid = oidm_find( at->at_syntax_oid );
344 *err = at->at_syntax_oid;
345 return SLAP_SCHERR_OIDM;
347 if ( oid != at->at_syntax_oid ) {
348 ldap_memfree( at->at_syntax_oid );
349 at->at_syntax_oid = oid;
353 if ( at->at_names && at->at_names[0] ) {
356 for( i=0; at->at_names[i]; i++ ) {
357 if( !slap_valid_descr( at->at_names[i] ) ) {
358 *err = at->at_names[i];
359 return SLAP_SCHERR_BAD_DESCR;
363 cname = at->at_names[0];
365 } else if ( at->at_oid ) {
370 return SLAP_SCHERR_ATTR_INCOMPLETE;
375 if ( !at->at_usage && at->at_no_user_mod ) {
376 /* user attribute must be modifable */
377 return SLAP_SCHERR_ATTR_BAD_USAGE;
380 if ( at->at_collective ) {
382 /* collective attributes cannot be operational */
383 return SLAP_SCHERR_ATTR_BAD_USAGE;
386 if( at->at_single_value ) {
387 /* collective attributes cannot be single-valued */
388 return SLAP_SCHERR_ATTR_BAD_USAGE;
392 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
393 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
395 sat->sat_cname.bv_val = cname;
396 sat->sat_cname.bv_len = strlen( cname );
397 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
399 if ( at->at_sup_oid ) {
400 AttributeType *supsat = at_find(at->at_sup_oid);
402 if ( supsat == NULL ) {
403 *err = at->at_sup_oid;
404 return SLAP_SCHERR_ATTR_NOT_FOUND;
407 sat->sat_sup = supsat;
409 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
410 return SLAP_SCHERR_OUTOFMEM;
413 if ( sat->sat_usage != supsat->sat_usage ) {
414 /* subtypes must have same usage as their SUP */
415 return SLAP_SCHERR_ATTR_BAD_USAGE;
418 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
419 /* subtypes must be obsolete if super is */
420 return SLAP_SCHERR_ATTR_BAD_SUP;
423 if ( sat->sat_flags & SLAP_AT_FINAL ) {
424 /* cannot subtype a "final" attribute type */
425 return SLAP_SCHERR_ATTR_BAD_SUP;
430 * Inherit definitions from superiors. We only check the
431 * direct superior since that one has already inherited from
434 if ( sat->sat_sup ) {
435 sat->sat_syntax = sat->sat_sup->sat_syntax;
436 sat->sat_equality = sat->sat_sup->sat_equality;
437 sat->sat_approx = sat->sat_sup->sat_approx;
438 sat->sat_ordering = sat->sat_sup->sat_ordering;
439 sat->sat_substr = sat->sat_sup->sat_substr;
443 * check for X-ORDERED attributes
445 if ( sat->sat_extensions ) {
446 for (i=0; sat->sat_extensions[i]; i++) {
447 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
449 sat->sat_flags |= SLAP_AT_ORDERED;
456 sat->sat_flags |= SLAP_AT_HARDCODE;
458 if ( at->at_syntax_oid ) {
459 syn = syn_find(sat->sat_syntax_oid);
461 *err = sat->sat_syntax_oid;
462 return SLAP_SCHERR_SYN_NOT_FOUND;
465 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
466 return SLAP_SCHERR_ATTR_BAD_SUP;
469 sat->sat_syntax = syn;
471 } else if ( sat->sat_syntax == NULL ) {
472 return SLAP_SCHERR_ATTR_INCOMPLETE;
475 if ( sat->sat_equality_oid ) {
476 mr = mr_find(sat->sat_equality_oid);
479 *err = sat->sat_equality_oid;
480 return SLAP_SCHERR_MR_NOT_FOUND;
483 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
484 *err = sat->sat_equality_oid;
485 return SLAP_SCHERR_ATTR_BAD_MR;
488 if( sat->sat_syntax != mr->smr_syntax ) {
489 if( mr->smr_compat_syntaxes == NULL ) {
490 *err = sat->sat_equality_oid;
491 return SLAP_SCHERR_ATTR_BAD_MR;
494 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
495 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
502 *err = sat->sat_equality_oid;
503 return SLAP_SCHERR_ATTR_BAD_MR;
507 sat->sat_equality = mr;
508 sat->sat_approx = mr->smr_associated;
511 if ( sat->sat_ordering_oid ) {
512 if( !sat->sat_equality ) {
513 *err = sat->sat_ordering_oid;
514 return SLAP_SCHERR_ATTR_BAD_MR;
517 mr = mr_find(sat->sat_ordering_oid);
520 *err = sat->sat_ordering_oid;
521 return SLAP_SCHERR_MR_NOT_FOUND;
524 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
525 *err = sat->sat_ordering_oid;
526 return SLAP_SCHERR_ATTR_BAD_MR;
529 if( sat->sat_syntax != mr->smr_syntax ) {
530 if( mr->smr_compat_syntaxes == NULL ) {
531 *err = sat->sat_ordering_oid;
532 return SLAP_SCHERR_ATTR_BAD_MR;
535 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
536 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
543 *err = sat->sat_ordering_oid;
544 return SLAP_SCHERR_ATTR_BAD_MR;
548 sat->sat_ordering = mr;
551 if ( sat->sat_substr_oid ) {
552 if( !sat->sat_equality ) {
553 *err = sat->sat_substr_oid;
554 return SLAP_SCHERR_ATTR_BAD_MR;
557 mr = mr_find(sat->sat_substr_oid);
560 *err = sat->sat_substr_oid;
561 return SLAP_SCHERR_MR_NOT_FOUND;
564 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
565 *err = sat->sat_substr_oid;
566 return SLAP_SCHERR_ATTR_BAD_MR;
569 /* due to funky LDAP builtin substring rules,
570 * we check against the equality rule assertion
571 * syntax and compat syntaxes instead of those
572 * associated with the substrings rule.
574 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
575 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
576 *err = sat->sat_substr_oid;
577 return SLAP_SCHERR_ATTR_BAD_MR;
580 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
581 if( sat->sat_syntax ==
582 sat->sat_equality->smr_compat_syntaxes[i] )
590 *err = sat->sat_substr_oid;
591 return SLAP_SCHERR_ATTR_BAD_MR;
595 sat->sat_substr = mr;
598 code = at_insert(sat,err);
599 if ( code == 0 && rsat )
606 at_index_printnode( void *v_air, void *ignore )
608 struct aindexrec *air = v_air;
610 air->air_name.bv_val,
611 ldap_attributetype2str(&air->air_at->sat_atype) );
616 at_index_print( void )
618 printf("Printing attribute type index:\n");
619 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
624 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
628 struct berval bv, *bva = NULL, idx;
632 start = LDAP_STAILQ_FIRST( &attr_list );
634 /* count the result size */
636 for ( at=start; at && at!=end; at=LDAP_STAILQ_NEXT(at, sat_next)) {
637 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
643 bva = ch_malloc( (num+1) * sizeof(struct berval) );
651 for ( at=start; at && at!=end; at=LDAP_STAILQ_NEXT(at, sat_next)) {
652 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
653 if ( ldap_attributetype2bv( &at->sat_atype, &bv ) == NULL ) {
654 ber_bvarray_free( bva );
657 idx.bv_len = sprintf(idx.bv_val, "{%02d}", i);
659 bva[i].bv_len = idx.bv_len + bv.bv_len;
660 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
661 strcpy( bva[i].bv_val, ibuf );
662 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
664 bva[i].bv_val = NULL;
665 ldap_memfree( bv.bv_val );
671 at_schema_info( Entry *e )
673 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
678 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
679 if( at->sat_flags & SLAP_AT_HIDE ) continue;
681 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
685 ber_str2bv( at->sat_oid, 0, 0, &nval );
687 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
691 ldap_memfree( val.bv_val );