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_SLIST_HEAD(ATList, slap_attribute_type) attr_list
62 = LDAP_SLIST_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_SLIST_EMPTY(&attr_list) ) {
214 a = LDAP_SLIST_FIRST(&attr_list);
215 LDAP_SLIST_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_SLIST_FIRST(&attr_list);
235 return (*at != NULL);
239 at_next( AttributeType **at )
243 #if 1 /* pedantic check */
245 AttributeType *tmp = NULL;
247 LDAP_SLIST_FOREACH(tmp,&attr_list,sat_next) {
257 *at = LDAP_SLIST_NEXT(*at,sat_next);
259 return (*at != NULL);
269 struct aindexrec *air;
272 LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
273 LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
275 if ( sat->sat_oid ) {
276 air = (struct aindexrec *)
277 ch_calloc( 1, sizeof(struct aindexrec) );
278 air->air_name.bv_val = sat->sat_oid;
279 air->air_name.bv_len = strlen(sat->sat_oid);
281 if ( avl_insert( &attr_index, (caddr_t) air,
282 attr_index_cmp, avl_dup_error ) ) {
285 return SLAP_SCHERR_ATTR_DUP;
287 /* FIX: temporal consistency check */
288 at_bvfind(&air->air_name);
291 if ( (names = sat->sat_names) ) {
293 air = (struct aindexrec *)
294 ch_calloc( 1, sizeof(struct aindexrec) );
295 air->air_name.bv_val = *names;
296 air->air_name.bv_len = strlen(*names);
298 if ( avl_insert( &attr_index, (caddr_t) air,
299 attr_index_cmp, avl_dup_error ) ) {
302 return SLAP_SCHERR_ATTR_DUP;
304 /* FIX: temporal consistency check */
305 at_bvfind(&air->air_name);
315 LDAPAttributeType *at,
326 if ( !OID_LEADCHAR( at->at_oid[0] )) {
327 /* Expand OID macros */
328 oid = oidm_find( at->at_oid );
331 return SLAP_SCHERR_OIDM;
333 if ( oid != at->at_oid ) {
334 ldap_memfree( at->at_oid );
339 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
340 /* Expand OID macros */
341 oid = oidm_find( at->at_syntax_oid );
343 *err = at->at_syntax_oid;
344 return SLAP_SCHERR_OIDM;
346 if ( oid != at->at_syntax_oid ) {
347 ldap_memfree( at->at_syntax_oid );
348 at->at_syntax_oid = oid;
352 if ( at->at_names && at->at_names[0] ) {
355 for( i=0; at->at_names[i]; i++ ) {
356 if( !slap_valid_descr( at->at_names[i] ) ) {
357 *err = at->at_names[i];
358 return SLAP_SCHERR_BAD_DESCR;
362 cname = at->at_names[0];
364 } else if ( at->at_oid ) {
369 return SLAP_SCHERR_ATTR_INCOMPLETE;
374 if ( !at->at_usage && at->at_no_user_mod ) {
375 /* user attribute must be modifable */
376 return SLAP_SCHERR_ATTR_BAD_USAGE;
379 if ( at->at_collective ) {
381 /* collective attributes cannot be operational */
382 return SLAP_SCHERR_ATTR_BAD_USAGE;
385 if( at->at_single_value ) {
386 /* collective attributes cannot be single-valued */
387 return SLAP_SCHERR_ATTR_BAD_USAGE;
391 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
392 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
394 sat->sat_cname.bv_val = cname;
395 sat->sat_cname.bv_len = strlen( cname );
396 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
398 if ( at->at_sup_oid ) {
399 AttributeType *supsat = at_find(at->at_sup_oid);
401 if ( supsat == NULL ) {
402 *err = at->at_sup_oid;
403 return SLAP_SCHERR_ATTR_NOT_FOUND;
406 sat->sat_sup = supsat;
408 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
409 return SLAP_SCHERR_OUTOFMEM;
412 if ( sat->sat_usage != supsat->sat_usage ) {
413 /* subtypes must have same usage as their SUP */
414 return SLAP_SCHERR_ATTR_BAD_USAGE;
417 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
418 /* subtypes must be obsolete if super is */
419 return SLAP_SCHERR_ATTR_BAD_SUP;
422 if ( sat->sat_flags & SLAP_AT_FINAL ) {
423 /* cannot subtype a "final" attribute type */
424 return SLAP_SCHERR_ATTR_BAD_SUP;
429 * Inherit definitions from superiors. We only check the
430 * direct superior since that one has already inherited from
433 if ( sat->sat_sup ) {
434 sat->sat_syntax = sat->sat_sup->sat_syntax;
435 sat->sat_equality = sat->sat_sup->sat_equality;
436 sat->sat_approx = sat->sat_sup->sat_approx;
437 sat->sat_ordering = sat->sat_sup->sat_ordering;
438 sat->sat_substr = sat->sat_sup->sat_substr;
441 if ( at->at_syntax_oid ) {
442 syn = syn_find(sat->sat_syntax_oid);
444 *err = sat->sat_syntax_oid;
445 return SLAP_SCHERR_SYN_NOT_FOUND;
448 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
449 return SLAP_SCHERR_ATTR_BAD_SUP;
452 sat->sat_syntax = syn;
454 } else if ( sat->sat_syntax == NULL ) {
455 return SLAP_SCHERR_ATTR_INCOMPLETE;
458 if ( sat->sat_equality_oid ) {
459 mr = mr_find(sat->sat_equality_oid);
462 *err = sat->sat_equality_oid;
463 return SLAP_SCHERR_MR_NOT_FOUND;
466 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
467 *err = sat->sat_equality_oid;
468 return SLAP_SCHERR_ATTR_BAD_MR;
471 if( sat->sat_syntax != mr->smr_syntax ) {
472 if( mr->smr_compat_syntaxes == NULL ) {
473 *err = sat->sat_equality_oid;
474 return SLAP_SCHERR_ATTR_BAD_MR;
477 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
478 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
485 *err = sat->sat_equality_oid;
486 return SLAP_SCHERR_ATTR_BAD_MR;
490 sat->sat_equality = mr;
491 sat->sat_approx = mr->smr_associated;
494 if ( sat->sat_ordering_oid ) {
495 if( !sat->sat_equality ) {
496 *err = sat->sat_ordering_oid;
497 return SLAP_SCHERR_ATTR_BAD_MR;
500 mr = mr_find(sat->sat_ordering_oid);
503 *err = sat->sat_ordering_oid;
504 return SLAP_SCHERR_MR_NOT_FOUND;
507 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
508 *err = sat->sat_ordering_oid;
509 return SLAP_SCHERR_ATTR_BAD_MR;
512 if( sat->sat_syntax != mr->smr_syntax ) {
513 if( mr->smr_compat_syntaxes == NULL ) {
514 *err = sat->sat_ordering_oid;
515 return SLAP_SCHERR_ATTR_BAD_MR;
518 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
519 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
526 *err = sat->sat_ordering_oid;
527 return SLAP_SCHERR_ATTR_BAD_MR;
531 sat->sat_ordering = mr;
534 if ( sat->sat_substr_oid ) {
535 if( !sat->sat_equality ) {
536 *err = sat->sat_substr_oid;
537 return SLAP_SCHERR_ATTR_BAD_MR;
540 mr = mr_find(sat->sat_substr_oid);
543 *err = sat->sat_substr_oid;
544 return SLAP_SCHERR_MR_NOT_FOUND;
547 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
548 *err = sat->sat_substr_oid;
549 return SLAP_SCHERR_ATTR_BAD_MR;
552 /* due to funky LDAP builtin substring rules,
553 * we check against the equality rule assertion
554 * syntax and compat syntaxes instead of those
555 * associated with the substrings rule.
557 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
558 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
559 *err = sat->sat_substr_oid;
560 return SLAP_SCHERR_ATTR_BAD_MR;
563 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
564 if( sat->sat_syntax ==
565 sat->sat_equality->smr_compat_syntaxes[i] )
573 *err = sat->sat_substr_oid;
574 return SLAP_SCHERR_ATTR_BAD_MR;
578 sat->sat_substr = mr;
581 code = at_insert(sat,err);
587 at_index_printnode( void *v_air, void *ignore )
589 struct aindexrec *air = v_air;
591 air->air_name.bv_val,
592 ldap_attributetype2str(&air->air_at->sat_atype) );
597 at_index_print( void )
599 printf("Printing attribute type index:\n");
600 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
605 at_schema_info( Entry *e )
607 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
612 LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
613 if( at->sat_flags & SLAP_AT_HIDE ) continue;
615 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
619 nval.bv_val = at->sat_oid;
620 nval.bv_len = strlen(at->sat_oid);
622 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
626 ldap_memfree( val.bv_val );