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,
328 if ( !OID_LEADCHAR( at->at_oid[0] )) {
329 /* Expand OID macros */
330 oid = oidm_find( at->at_oid );
333 return SLAP_SCHERR_OIDM;
335 if ( oid != at->at_oid ) {
341 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
342 /* Expand OID macros */
343 oid = oidm_find( at->at_syntax_oid );
345 *err = at->at_syntax_oid;
346 return SLAP_SCHERR_OIDM;
348 if ( oid != at->at_syntax_oid ) {
349 ldap_memfree( at->at_syntax_oid );
350 at->at_syntax_oid = oid;
354 if ( at->at_names && at->at_names[0] ) {
357 for( i=0; at->at_names[i]; i++ ) {
358 if( !slap_valid_descr( at->at_names[i] ) ) {
359 *err = at->at_names[i];
360 return SLAP_SCHERR_BAD_DESCR;
364 cname = at->at_names[0];
366 } else if ( at->at_oid ) {
371 return SLAP_SCHERR_ATTR_INCOMPLETE;
376 if ( !at->at_usage && at->at_no_user_mod ) {
377 /* user attribute must be modifable */
378 return SLAP_SCHERR_ATTR_BAD_USAGE;
381 if ( at->at_collective ) {
383 /* collective attributes cannot be operational */
384 return SLAP_SCHERR_ATTR_BAD_USAGE;
387 if( at->at_single_value ) {
388 /* collective attributes cannot be single-valued */
389 return SLAP_SCHERR_ATTR_BAD_USAGE;
393 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
394 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
396 sat->sat_cname.bv_val = cname;
397 sat->sat_cname.bv_len = strlen( cname );
398 sat->sat_oidmacro = oidm;
399 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
401 if ( at->at_sup_oid ) {
402 AttributeType *supsat = at_find(at->at_sup_oid);
404 if ( supsat == NULL ) {
405 *err = at->at_sup_oid;
406 return SLAP_SCHERR_ATTR_NOT_FOUND;
409 sat->sat_sup = supsat;
411 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
412 return SLAP_SCHERR_OUTOFMEM;
415 if ( sat->sat_usage != supsat->sat_usage ) {
416 /* subtypes must have same usage as their SUP */
417 return SLAP_SCHERR_ATTR_BAD_USAGE;
420 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
421 /* subtypes must be obsolete if super is */
422 return SLAP_SCHERR_ATTR_BAD_SUP;
425 if ( sat->sat_flags & SLAP_AT_FINAL ) {
426 /* cannot subtype a "final" attribute type */
427 return SLAP_SCHERR_ATTR_BAD_SUP;
432 * Inherit definitions from superiors. We only check the
433 * direct superior since that one has already inherited from
436 if ( sat->sat_sup ) {
437 sat->sat_syntax = sat->sat_sup->sat_syntax;
438 sat->sat_equality = sat->sat_sup->sat_equality;
439 sat->sat_approx = sat->sat_sup->sat_approx;
440 sat->sat_ordering = sat->sat_sup->sat_ordering;
441 sat->sat_substr = sat->sat_sup->sat_substr;
445 * check for X-ORDERED attributes
447 if ( sat->sat_extensions ) {
448 for (i=0; sat->sat_extensions[i]; i++) {
449 if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
451 sat->sat_flags |= SLAP_AT_ORDERED;
458 sat->sat_flags |= SLAP_AT_HARDCODE;
460 if ( at->at_syntax_oid ) {
461 syn = syn_find(sat->sat_syntax_oid);
463 *err = sat->sat_syntax_oid;
464 return SLAP_SCHERR_SYN_NOT_FOUND;
467 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
468 return SLAP_SCHERR_ATTR_BAD_SUP;
471 sat->sat_syntax = syn;
473 } else if ( sat->sat_syntax == NULL ) {
474 return SLAP_SCHERR_ATTR_INCOMPLETE;
477 if ( sat->sat_equality_oid ) {
478 mr = mr_find(sat->sat_equality_oid);
481 *err = sat->sat_equality_oid;
482 return SLAP_SCHERR_MR_NOT_FOUND;
485 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
486 *err = sat->sat_equality_oid;
487 return SLAP_SCHERR_ATTR_BAD_MR;
490 if( sat->sat_syntax != mr->smr_syntax ) {
491 if( mr->smr_compat_syntaxes == NULL ) {
492 *err = sat->sat_equality_oid;
493 return SLAP_SCHERR_ATTR_BAD_MR;
496 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
497 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
504 *err = sat->sat_equality_oid;
505 return SLAP_SCHERR_ATTR_BAD_MR;
509 sat->sat_equality = mr;
510 sat->sat_approx = mr->smr_associated;
513 if ( sat->sat_ordering_oid ) {
514 if( !sat->sat_equality ) {
515 *err = sat->sat_ordering_oid;
516 return SLAP_SCHERR_ATTR_BAD_MR;
519 mr = mr_find(sat->sat_ordering_oid);
522 *err = sat->sat_ordering_oid;
523 return SLAP_SCHERR_MR_NOT_FOUND;
526 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
527 *err = sat->sat_ordering_oid;
528 return SLAP_SCHERR_ATTR_BAD_MR;
531 if( sat->sat_syntax != mr->smr_syntax ) {
532 if( mr->smr_compat_syntaxes == NULL ) {
533 *err = sat->sat_ordering_oid;
534 return SLAP_SCHERR_ATTR_BAD_MR;
537 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
538 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
545 *err = sat->sat_ordering_oid;
546 return SLAP_SCHERR_ATTR_BAD_MR;
550 sat->sat_ordering = mr;
553 if ( sat->sat_substr_oid ) {
554 if( !sat->sat_equality ) {
555 *err = sat->sat_substr_oid;
556 return SLAP_SCHERR_ATTR_BAD_MR;
559 mr = mr_find(sat->sat_substr_oid);
562 *err = sat->sat_substr_oid;
563 return SLAP_SCHERR_MR_NOT_FOUND;
566 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
567 *err = sat->sat_substr_oid;
568 return SLAP_SCHERR_ATTR_BAD_MR;
571 /* due to funky LDAP builtin substring rules,
572 * we check against the equality rule assertion
573 * syntax and compat syntaxes instead of those
574 * associated with the substrings rule.
576 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
577 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
578 *err = sat->sat_substr_oid;
579 return SLAP_SCHERR_ATTR_BAD_MR;
582 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
583 if( sat->sat_syntax ==
584 sat->sat_equality->smr_compat_syntaxes[i] )
592 *err = sat->sat_substr_oid;
593 return SLAP_SCHERR_ATTR_BAD_MR;
597 sat->sat_substr = mr;
600 code = at_insert(sat,err);
601 if ( code == 0 && rsat )
608 at_index_printnode( void *v_air, void *ignore )
610 struct aindexrec *air = v_air;
612 air->air_name.bv_val,
613 ldap_attributetype2str(&air->air_at->sat_atype) );
618 at_index_print( void )
620 printf("Printing attribute type index:\n");
621 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
626 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
630 struct berval bv, *bva = NULL, idx;
634 start = LDAP_STAILQ_FIRST( &attr_list );
636 /* count the result size */
638 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
639 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
641 if ( at == end ) break;
646 bva = ch_malloc( (num+1) * sizeof(struct berval) );
654 for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
655 LDAPAttributeType lat, *latp;
656 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
657 if ( at->sat_oidmacro ) {
659 lat.at_oid = at->sat_oidmacro;
662 latp = &at->sat_atype;
664 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
665 ber_bvarray_free( bva );
668 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
670 bva[i].bv_len = idx.bv_len + bv.bv_len;
671 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
672 strcpy( bva[i].bv_val, ibuf );
673 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
675 bva[i].bv_val = NULL;
676 ldap_memfree( bv.bv_val );
677 if ( at == end ) break;
683 at_schema_info( Entry *e )
685 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
690 LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
691 if( at->sat_flags & SLAP_AT_HIDE ) continue;
693 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
697 ber_str2bv( at->sat_oid, 0, 0, &nval );
699 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
703 ldap_memfree( val.bv_val );