3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* at.c - routines for dealing with attribute types */
14 #include <ac/socket.h>
15 #include <ac/string.h>
26 for( ; at != NULL; at = at->sat_sup ) {
27 if( at->sat_syntax_oid ) {
28 return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
39 for( ; sub != NULL; sub = sub->sat_sup ) {
40 if( sub == sup ) return 1;
47 struct berval air_name;
48 AttributeType *air_at;
51 static Avlnode *attr_index = NULL;
52 static LDAP_SLIST_HEAD(ATList, slap_attribute_type) attr_list
53 = LDAP_SLIST_HEAD_INITIALIZER(&attr_list);
61 const struct aindexrec *air1 = v_air1;
62 const struct aindexrec *air2 = v_air2;
63 int i = air1->air_name.bv_len - air2->air_name.bv_len;
66 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
75 const struct berval *type = v_type;
76 const struct aindexrec *air = v_air;
77 int i = type->bv_len - air->air_name.bv_len;
80 return (strncasecmp( type->bv_val, air->air_name.bv_val,
91 bv.bv_val = (char *)name;
92 bv.bv_len = strlen( name );
94 return at_bvfind( &bv );
102 struct aindexrec *air;
104 air = avl_find( attr_index, name, attr_index_name_cmp );
106 return air != NULL ? air->air_at : NULL;
112 AttributeType ***listp
115 AttributeType **list;
116 AttributeType **list1;
122 list = ch_calloc(size, sizeof(AttributeType *));
134 list1 = ch_realloc(list, size*sizeof(AttributeType *));
149 AttributeType ***listp
152 AttributeType **list;
153 AttributeType **list1;
161 for ( i=0; list[i]; i++ )
166 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
170 /* Tell the runtime this can be shrinked */
171 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
190 for ( i=0; list[i]; i++ ) {
191 if ( sat == list[i] ) {
202 avl_free(attr_index, ldap_memfree);
204 while( !LDAP_SLIST_EMPTY(&attr_list) ) {
205 a = LDAP_SLIST_FIRST(&attr_list);
206 LDAP_SLIST_REMOVE_HEAD(&attr_list, sat_next);
208 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
209 ad_destroy(a->sat_ad);
210 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
211 ldap_attributetype_free((LDAPAttributeType *)a);
214 if ( slap_schema.si_at_undefined ) {
215 ad_destroy(slap_schema.si_at_undefined->sat_ad);
220 at_start( AttributeType **at )
224 *at = LDAP_SLIST_FIRST(&attr_list);
226 return (*at != NULL);
230 at_next( AttributeType **at )
234 #if 1 /* pedantic check */
236 AttributeType *tmp = NULL;
238 LDAP_SLIST_FOREACH(tmp,&attr_list,sat_next) {
248 *at = LDAP_SLIST_NEXT(*at,sat_next);
250 return (*at != NULL);
261 struct aindexrec *air;
264 LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
266 if ( sat->sat_oid ) {
267 air = (struct aindexrec *)
268 ch_calloc( 1, sizeof(struct aindexrec) );
269 air->air_name.bv_val = sat->sat_oid;
270 air->air_name.bv_len = strlen(sat->sat_oid);
272 if ( avl_insert( &attr_index, (caddr_t) air,
273 attr_index_cmp, avl_dup_error ) ) {
276 return SLAP_SCHERR_ATTR_DUP;
278 /* FIX: temporal consistency check */
279 at_bvfind(&air->air_name);
282 if ( (names = sat->sat_names) ) {
284 air = (struct aindexrec *)
285 ch_calloc( 1, sizeof(struct aindexrec) );
286 air->air_name.bv_val = *names;
287 air->air_name.bv_len = strlen(*names);
289 if ( avl_insert( &attr_index, (caddr_t) air,
290 attr_index_cmp, avl_dup_error ) ) {
293 return SLAP_SCHERR_ATTR_DUP;
295 /* FIX: temporal consistency check */
296 at_bvfind(&air->air_name);
306 LDAPAttributeType *at,
318 if ( !OID_LEADCHAR( at->at_oid[0] )) {
319 /* Expand OID macros */
320 oid = oidm_find( at->at_oid );
323 return SLAP_SCHERR_OIDM;
325 if ( oid != at->at_oid ) {
326 ldap_memfree( at->at_oid );
331 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
332 /* Expand OID macros */
333 oid = oidm_find( at->at_syntax_oid );
335 *err = at->at_syntax_oid;
336 return SLAP_SCHERR_OIDM;
338 if ( oid != at->at_syntax_oid ) {
339 ldap_memfree( at->at_syntax_oid );
340 at->at_syntax_oid = oid;
344 if ( at->at_names && at->at_names[0] ) {
347 for( i=0; at->at_names[i]; i++ ) {
348 if( !slap_valid_descr( at->at_names[i] ) ) {
349 *err = at->at_names[i];
350 return SLAP_SCHERR_BAD_DESCR;
354 cname = at->at_names[0];
356 } else if ( at->at_oid ) {
361 return SLAP_SCHERR_ATTR_INCOMPLETE;
366 if ( !at->at_usage && at->at_no_user_mod ) {
367 /* user attribute must be modifable */
368 return SLAP_SCHERR_ATTR_BAD_USAGE;
371 if ( at->at_collective ) {
373 /* collective attributes cannot be operational */
374 return SLAP_SCHERR_ATTR_BAD_USAGE;
377 if( at->at_single_value ) {
378 /* collective attributes cannot be single-valued */
379 return SLAP_SCHERR_ATTR_BAD_USAGE;
383 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
384 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
386 sat->sat_cname.bv_val = cname;
387 sat->sat_cname.bv_len = strlen( cname );
388 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
390 if ( at->at_sup_oid ) {
391 AttributeType *supsat = at_find(at->at_sup_oid);
393 if ( supsat == NULL ) {
394 *err = at->at_sup_oid;
395 return SLAP_SCHERR_ATTR_NOT_FOUND;
398 sat->sat_sup = supsat;
400 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
401 return SLAP_SCHERR_OUTOFMEM;
404 if ( sat->sat_usage != supsat->sat_usage ) {
405 /* subtypes must have same usage as their SUP */
406 return SLAP_SCHERR_ATTR_BAD_USAGE;
409 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
410 /* subtypes must be obsolete if super is */
411 return SLAP_SCHERR_ATTR_BAD_SUP;
414 if ( sat->sat_flags & SLAP_AT_FINAL ) {
415 /* cannot subtype a "final" attribute type */
416 return SLAP_SCHERR_ATTR_BAD_SUP;
421 * Inherit definitions from superiors. We only check the
422 * direct superior since that one has already inherited from
425 if ( sat->sat_sup ) {
426 sat->sat_syntax = sat->sat_sup->sat_syntax;
427 sat->sat_equality = sat->sat_sup->sat_equality;
428 sat->sat_approx = sat->sat_sup->sat_approx;
429 sat->sat_ordering = sat->sat_sup->sat_ordering;
430 sat->sat_substr = sat->sat_sup->sat_substr;
433 if ( at->at_syntax_oid ) {
434 syn = syn_find(sat->sat_syntax_oid);
436 *err = sat->sat_syntax_oid;
437 return SLAP_SCHERR_SYN_NOT_FOUND;
440 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
441 return SLAP_SCHERR_ATTR_BAD_SUP;
444 sat->sat_syntax = syn;
446 } else if ( sat->sat_syntax == NULL ) {
447 return SLAP_SCHERR_ATTR_INCOMPLETE;
450 if ( sat->sat_equality_oid ) {
451 mr = mr_find(sat->sat_equality_oid);
454 *err = sat->sat_equality_oid;
455 return SLAP_SCHERR_MR_NOT_FOUND;
458 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
459 *err = sat->sat_equality_oid;
460 return SLAP_SCHERR_ATTR_BAD_MR;
463 if( sat->sat_syntax != mr->smr_syntax ) {
464 if( mr->smr_compat_syntaxes == NULL ) {
465 *err = sat->sat_equality_oid;
466 return SLAP_SCHERR_ATTR_BAD_MR;
469 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
470 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
477 *err = sat->sat_equality_oid;
478 return SLAP_SCHERR_ATTR_BAD_MR;
482 sat->sat_equality = mr;
483 sat->sat_approx = mr->smr_associated;
486 if ( sat->sat_ordering_oid ) {
487 mr = mr_find(sat->sat_ordering_oid);
490 *err = sat->sat_ordering_oid;
491 return SLAP_SCHERR_MR_NOT_FOUND;
494 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
495 *err = sat->sat_ordering_oid;
496 return SLAP_SCHERR_ATTR_BAD_MR;
499 if( sat->sat_syntax != mr->smr_syntax ) {
500 if( mr->smr_compat_syntaxes == NULL ) {
501 *err = sat->sat_ordering_oid;
502 return SLAP_SCHERR_ATTR_BAD_MR;
505 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
506 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
513 *err = sat->sat_ordering_oid;
514 return SLAP_SCHERR_ATTR_BAD_MR;
518 sat->sat_ordering = mr;
521 if ( sat->sat_substr_oid ) {
522 mr = mr_find(sat->sat_substr_oid);
525 *err = sat->sat_substr_oid;
526 return SLAP_SCHERR_MR_NOT_FOUND;
529 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
530 *err = sat->sat_substr_oid;
531 return SLAP_SCHERR_ATTR_BAD_MR;
534 /* due to funky LDAP builtin substring rules, we
535 * we check against the equality rule assertion
536 * syntax and compat syntaxes instead of those
537 * associated with the substrings rule.
539 if( sat->sat_equality &&
540 sat->sat_syntax != sat->sat_equality->smr_syntax )
542 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
543 *err = sat->sat_substr_oid;
544 return SLAP_SCHERR_ATTR_BAD_MR;
547 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
548 if( sat->sat_syntax ==
549 sat->sat_equality->smr_compat_syntaxes[i] )
557 *err = sat->sat_substr_oid;
558 return SLAP_SCHERR_ATTR_BAD_MR;
562 sat->sat_substr = mr;
565 code = at_insert(sat,err);
571 at_index_printnode( void *v_air, void *ignore )
573 struct aindexrec *air = v_air;
575 air->air_name.bv_val,
576 ldap_attributetype2str(&air->air_at->sat_atype) );
581 at_index_print( void )
583 printf("Printing attribute type index:\n");
584 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
589 at_schema_info( Entry *e )
591 struct berval vals[2];
594 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
596 vals[1].bv_val = NULL;
598 LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
599 if( at->sat_flags & SLAP_AT_HIDE ) continue;
601 if ( ldap_attributetype2bv( &at->sat_atype, vals ) == NULL ) {
606 if( attr_merge( e, ad_attributeTypes, vals, NULL /* FIXME */ ) )
608 if( attr_merge( e, ad_attributeTypes, vals ) )
613 ldap_memfree( vals[0].bv_val );