1 /* at.c - routines for dealing with attribute types */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>
35 for( ; at != NULL; at = at->sat_sup ) {
36 if( at->sat_syntax_oid ) {
37 return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
48 for( ; sub != NULL; sub = sub->sat_sup ) {
49 if( sub == sup ) return 1;
56 struct berval air_name;
57 AttributeType *air_at;
60 static Avlnode *attr_index = NULL;
61 static LDAP_SLIST_HEAD(ATList, slap_attribute_type) attr_list
62 = LDAP_SLIST_HEAD_INITIALIZER(&attr_list);
70 const struct aindexrec *air1 = v_air1;
71 const struct aindexrec *air2 = v_air2;
72 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 ));
84 const struct berval *type = v_type;
85 const struct aindexrec *air = v_air;
86 int i = type->bv_len - air->air_name.bv_len;
89 return (strncasecmp( type->bv_val, air->air_name.bv_val,
100 bv.bv_val = (char *)name;
101 bv.bv_len = strlen( name );
103 return at_bvfind( &bv );
111 struct aindexrec *air;
113 air = avl_find( attr_index, name, attr_index_name_cmp );
115 return air != NULL ? air->air_at : NULL;
121 AttributeType ***listp
124 AttributeType **list;
125 AttributeType **list1;
131 list = ch_calloc(size, sizeof(AttributeType *));
143 list1 = ch_realloc(list, size*sizeof(AttributeType *));
158 AttributeType ***listp
161 AttributeType **list;
162 AttributeType **list1;
170 for ( i=0; list[i]; i++ )
175 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
179 /* Tell the runtime this can be shrinked */
180 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
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);
270 struct aindexrec *air;
273 LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
274 LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
276 if ( sat->sat_oid ) {
277 air = (struct aindexrec *)
278 ch_calloc( 1, sizeof(struct aindexrec) );
279 air->air_name.bv_val = sat->sat_oid;
280 air->air_name.bv_len = strlen(sat->sat_oid);
282 if ( avl_insert( &attr_index, (caddr_t) air,
283 attr_index_cmp, avl_dup_error ) ) {
286 return SLAP_SCHERR_ATTR_DUP;
288 /* FIX: temporal consistency check */
289 at_bvfind(&air->air_name);
292 if ( (names = sat->sat_names) ) {
294 air = (struct aindexrec *)
295 ch_calloc( 1, sizeof(struct aindexrec) );
296 air->air_name.bv_val = *names;
297 air->air_name.bv_len = strlen(*names);
299 if ( avl_insert( &attr_index, (caddr_t) air,
300 attr_index_cmp, avl_dup_error ) ) {
303 return SLAP_SCHERR_ATTR_DUP;
305 /* FIX: temporal consistency check */
306 at_bvfind(&air->air_name);
316 LDAPAttributeType *at,
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 ) {
336 ldap_memfree( 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 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
400 if ( at->at_sup_oid ) {
401 AttributeType *supsat = at_find(at->at_sup_oid);
403 if ( supsat == NULL ) {
404 *err = at->at_sup_oid;
405 return SLAP_SCHERR_ATTR_NOT_FOUND;
408 sat->sat_sup = supsat;
410 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
411 return SLAP_SCHERR_OUTOFMEM;
414 if ( sat->sat_usage != supsat->sat_usage ) {
415 /* subtypes must have same usage as their SUP */
416 return SLAP_SCHERR_ATTR_BAD_USAGE;
419 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
420 /* subtypes must be obsolete if super is */
421 return SLAP_SCHERR_ATTR_BAD_SUP;
424 if ( sat->sat_flags & SLAP_AT_FINAL ) {
425 /* cannot subtype a "final" attribute type */
426 return SLAP_SCHERR_ATTR_BAD_SUP;
431 * Inherit definitions from superiors. We only check the
432 * direct superior since that one has already inherited from
435 if ( sat->sat_sup ) {
436 sat->sat_syntax = sat->sat_sup->sat_syntax;
437 sat->sat_equality = sat->sat_sup->sat_equality;
438 sat->sat_approx = sat->sat_sup->sat_approx;
439 sat->sat_ordering = sat->sat_sup->sat_ordering;
440 sat->sat_substr = sat->sat_sup->sat_substr;
443 if ( at->at_syntax_oid ) {
444 syn = syn_find(sat->sat_syntax_oid);
446 *err = sat->sat_syntax_oid;
447 return SLAP_SCHERR_SYN_NOT_FOUND;
450 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
451 return SLAP_SCHERR_ATTR_BAD_SUP;
454 sat->sat_syntax = syn;
456 } else if ( sat->sat_syntax == NULL ) {
457 return SLAP_SCHERR_ATTR_INCOMPLETE;
460 if ( sat->sat_equality_oid ) {
461 mr = mr_find(sat->sat_equality_oid);
464 *err = sat->sat_equality_oid;
465 return SLAP_SCHERR_MR_NOT_FOUND;
468 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
469 *err = sat->sat_equality_oid;
470 return SLAP_SCHERR_ATTR_BAD_MR;
473 if( sat->sat_syntax != mr->smr_syntax ) {
474 if( mr->smr_compat_syntaxes == NULL ) {
475 *err = sat->sat_equality_oid;
476 return SLAP_SCHERR_ATTR_BAD_MR;
479 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
480 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
487 *err = sat->sat_equality_oid;
488 return SLAP_SCHERR_ATTR_BAD_MR;
492 sat->sat_equality = mr;
493 sat->sat_approx = mr->smr_associated;
496 if ( sat->sat_ordering_oid ) {
497 if( !sat->sat_equality ) {
498 *err = sat->sat_ordering_oid;
499 return SLAP_SCHERR_ATTR_BAD_MR;
502 mr = mr_find(sat->sat_ordering_oid);
505 *err = sat->sat_ordering_oid;
506 return SLAP_SCHERR_MR_NOT_FOUND;
509 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
510 *err = sat->sat_ordering_oid;
511 return SLAP_SCHERR_ATTR_BAD_MR;
514 if( sat->sat_syntax != mr->smr_syntax ) {
515 if( mr->smr_compat_syntaxes == NULL ) {
516 *err = sat->sat_ordering_oid;
517 return SLAP_SCHERR_ATTR_BAD_MR;
520 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
521 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
528 *err = sat->sat_ordering_oid;
529 return SLAP_SCHERR_ATTR_BAD_MR;
533 sat->sat_ordering = mr;
536 if ( sat->sat_substr_oid ) {
537 if( !sat->sat_equality ) {
538 *err = sat->sat_substr_oid;
539 return SLAP_SCHERR_ATTR_BAD_MR;
542 mr = mr_find(sat->sat_substr_oid);
545 *err = sat->sat_substr_oid;
546 return SLAP_SCHERR_MR_NOT_FOUND;
549 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
550 *err = sat->sat_substr_oid;
551 return SLAP_SCHERR_ATTR_BAD_MR;
554 /* due to funky LDAP builtin substring rules, we
555 * we check against the equality rule assertion
556 * syntax and compat syntaxes instead of those
557 * associated with the substrings rule.
559 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
560 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
561 *err = sat->sat_substr_oid;
562 return SLAP_SCHERR_ATTR_BAD_MR;
565 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
566 if( sat->sat_syntax ==
567 sat->sat_equality->smr_compat_syntaxes[i] )
575 *err = sat->sat_substr_oid;
576 return SLAP_SCHERR_ATTR_BAD_MR;
580 sat->sat_substr = mr;
583 code = at_insert(sat,err);
589 at_index_printnode( void *v_air, void *ignore )
591 struct aindexrec *air = v_air;
593 air->air_name.bv_val,
594 ldap_attributetype2str(&air->air_at->sat_atype) );
599 at_index_print( void )
601 printf("Printing attribute type index:\n");
602 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
607 at_schema_info( Entry *e )
609 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
614 LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
615 if( at->sat_flags & SLAP_AT_HIDE ) continue;
617 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
621 nval.bv_val = at->sat_oid;
622 nval.bv_len = strlen(at->sat_oid);
624 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
628 ldap_memfree( val.bv_val );