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>
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 LDAP_SLIST_HEAD(ATList, slap_attribute_type) attr_list
61 = LDAP_SLIST_HEAD_INITIALIZER(&attr_list);
68 const struct aindexrec *air1 = v_air1;
69 const struct aindexrec *air2 = v_air2;
70 int i = air1->air_name.bv_len - air2->air_name.bv_len;
72 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
80 const struct berval *type = v_type;
81 const struct aindexrec *air = v_air;
82 int i = type->bv_len - air->air_name.bv_len;
84 return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
88 at_find( const char *name )
92 bv.bv_val = (char *)name;
93 bv.bv_len = strlen( name );
95 return at_bvfind( &bv );
99 at_bvfind( struct berval *name )
101 struct aindexrec *air;
103 air = avl_find( attr_index, name, attr_index_name_cmp );
105 return air != NULL ? air->air_at : NULL;
111 AttributeType ***listp )
113 AttributeType **list;
114 AttributeType **list1;
120 list = ch_calloc(size, sizeof(AttributeType *));
132 list1 = ch_realloc(list, size*sizeof(AttributeType *));
147 AttributeType ***listp )
149 AttributeType **list;
150 AttributeType **list1;
158 for ( i=0; list[i]; i++ )
163 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
167 /* Tell the runtime this can be shrinked */
168 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
179 AttributeType **list )
186 for ( i=0; list[i]; i++ ) {
187 if ( sat == list[i] ) {
198 avl_free(attr_index, ldap_memfree);
200 while( !LDAP_SLIST_EMPTY(&attr_list) ) {
201 a = LDAP_SLIST_FIRST(&attr_list);
202 LDAP_SLIST_REMOVE_HEAD(&attr_list, sat_next);
204 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
205 ad_destroy(a->sat_ad);
206 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
207 ldap_attributetype_free((LDAPAttributeType *)a);
210 if ( slap_schema.si_at_undefined ) {
211 ad_destroy(slap_schema.si_at_undefined->sat_ad);
216 at_start( AttributeType **at )
220 *at = LDAP_SLIST_FIRST(&attr_list);
222 return (*at != NULL);
226 at_next( AttributeType **at )
230 #if 1 /* pedantic check */
232 AttributeType *tmp = NULL;
234 LDAP_SLIST_FOREACH(tmp,&attr_list,sat_next) {
244 *at = LDAP_SLIST_NEXT(*at,sat_next);
246 return (*at != NULL);
256 struct aindexrec *air;
259 LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
260 LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
262 if ( sat->sat_oid ) {
263 air = (struct aindexrec *)
264 ch_calloc( 1, sizeof(struct aindexrec) );
265 air->air_name.bv_val = sat->sat_oid;
266 air->air_name.bv_len = strlen(sat->sat_oid);
268 if ( avl_insert( &attr_index, (caddr_t) air,
269 attr_index_cmp, avl_dup_error ) ) {
272 return SLAP_SCHERR_ATTR_DUP;
274 /* FIX: temporal consistency check */
275 at_bvfind(&air->air_name);
278 if ( (names = sat->sat_names) ) {
280 air = (struct aindexrec *)
281 ch_calloc( 1, sizeof(struct aindexrec) );
282 air->air_name.bv_val = *names;
283 air->air_name.bv_len = strlen(*names);
285 if ( avl_insert( &attr_index, (caddr_t) air,
286 attr_index_cmp, avl_dup_error ) ) {
289 return SLAP_SCHERR_ATTR_DUP;
291 /* FIX: temporal consistency check */
292 at_bvfind(&air->air_name);
302 LDAPAttributeType *at,
313 if ( !OID_LEADCHAR( at->at_oid[0] )) {
314 /* Expand OID macros */
315 oid = oidm_find( at->at_oid );
318 return SLAP_SCHERR_OIDM;
320 if ( oid != at->at_oid ) {
321 ldap_memfree( at->at_oid );
326 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
327 /* Expand OID macros */
328 oid = oidm_find( at->at_syntax_oid );
330 *err = at->at_syntax_oid;
331 return SLAP_SCHERR_OIDM;
333 if ( oid != at->at_syntax_oid ) {
334 ldap_memfree( at->at_syntax_oid );
335 at->at_syntax_oid = oid;
339 if ( at->at_names && at->at_names[0] ) {
342 for( i=0; at->at_names[i]; i++ ) {
343 if( !slap_valid_descr( at->at_names[i] ) ) {
344 *err = at->at_names[i];
345 return SLAP_SCHERR_BAD_DESCR;
349 cname = at->at_names[0];
351 } else if ( at->at_oid ) {
356 return SLAP_SCHERR_ATTR_INCOMPLETE;
361 if ( !at->at_usage && at->at_no_user_mod ) {
362 /* user attribute must be modifable */
363 return SLAP_SCHERR_ATTR_BAD_USAGE;
366 if ( at->at_collective ) {
368 /* collective attributes cannot be operational */
369 return SLAP_SCHERR_ATTR_BAD_USAGE;
372 if( at->at_single_value ) {
373 /* collective attributes cannot be single-valued */
374 return SLAP_SCHERR_ATTR_BAD_USAGE;
378 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
379 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
381 sat->sat_cname.bv_val = cname;
382 sat->sat_cname.bv_len = strlen( cname );
383 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
385 if ( at->at_sup_oid ) {
386 AttributeType *supsat = at_find(at->at_sup_oid);
388 if ( supsat == NULL ) {
389 *err = at->at_sup_oid;
390 return SLAP_SCHERR_ATTR_NOT_FOUND;
393 sat->sat_sup = supsat;
395 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
396 return SLAP_SCHERR_OUTOFMEM;
399 if ( sat->sat_usage != supsat->sat_usage ) {
400 /* subtypes must have same usage as their SUP */
401 return SLAP_SCHERR_ATTR_BAD_USAGE;
404 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
405 /* subtypes must be obsolete if super is */
406 return SLAP_SCHERR_ATTR_BAD_SUP;
409 if ( sat->sat_flags & SLAP_AT_FINAL ) {
410 /* cannot subtype a "final" attribute type */
411 return SLAP_SCHERR_ATTR_BAD_SUP;
416 * Inherit definitions from superiors. We only check the
417 * direct superior since that one has already inherited from
420 if ( sat->sat_sup ) {
421 sat->sat_syntax = sat->sat_sup->sat_syntax;
422 sat->sat_equality = sat->sat_sup->sat_equality;
423 sat->sat_approx = sat->sat_sup->sat_approx;
424 sat->sat_ordering = sat->sat_sup->sat_ordering;
425 sat->sat_substr = sat->sat_sup->sat_substr;
428 if ( at->at_syntax_oid ) {
429 syn = syn_find(sat->sat_syntax_oid);
431 *err = sat->sat_syntax_oid;
432 return SLAP_SCHERR_SYN_NOT_FOUND;
435 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
436 return SLAP_SCHERR_ATTR_BAD_SUP;
439 sat->sat_syntax = syn;
441 } else if ( sat->sat_syntax == NULL ) {
442 return SLAP_SCHERR_ATTR_INCOMPLETE;
445 if ( sat->sat_equality_oid ) {
446 mr = mr_find(sat->sat_equality_oid);
449 *err = sat->sat_equality_oid;
450 return SLAP_SCHERR_MR_NOT_FOUND;
453 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
454 *err = sat->sat_equality_oid;
455 return SLAP_SCHERR_ATTR_BAD_MR;
458 if( sat->sat_syntax != mr->smr_syntax ) {
459 if( mr->smr_compat_syntaxes == NULL ) {
460 *err = sat->sat_equality_oid;
461 return SLAP_SCHERR_ATTR_BAD_MR;
464 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
465 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
472 *err = sat->sat_equality_oid;
473 return SLAP_SCHERR_ATTR_BAD_MR;
477 sat->sat_equality = mr;
478 sat->sat_approx = mr->smr_associated;
481 if ( sat->sat_ordering_oid ) {
482 if( !sat->sat_equality ) {
483 *err = sat->sat_ordering_oid;
484 return SLAP_SCHERR_ATTR_BAD_MR;
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 if( !sat->sat_equality ) {
523 *err = sat->sat_substr_oid;
524 return SLAP_SCHERR_ATTR_BAD_MR;
527 mr = mr_find(sat->sat_substr_oid);
530 *err = sat->sat_substr_oid;
531 return SLAP_SCHERR_MR_NOT_FOUND;
534 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
535 *err = sat->sat_substr_oid;
536 return SLAP_SCHERR_ATTR_BAD_MR;
539 /* due to funky LDAP builtin substring rules,
540 * we check against the equality rule assertion
541 * syntax and compat syntaxes instead of those
542 * associated with the substrings rule.
544 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
545 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
546 *err = sat->sat_substr_oid;
547 return SLAP_SCHERR_ATTR_BAD_MR;
550 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
551 if( sat->sat_syntax ==
552 sat->sat_equality->smr_compat_syntaxes[i] )
560 *err = sat->sat_substr_oid;
561 return SLAP_SCHERR_ATTR_BAD_MR;
565 sat->sat_substr = mr;
568 code = at_insert(sat,err);
574 at_index_printnode( void *v_air, void *ignore )
576 struct aindexrec *air = v_air;
578 air->air_name.bv_val,
579 ldap_attributetype2str(&air->air_at->sat_atype) );
584 at_index_print( void )
586 printf("Printing attribute type index:\n");
587 (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
592 at_schema_info( Entry *e )
594 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
599 LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
600 if( at->sat_flags & SLAP_AT_HIDE ) continue;
602 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
606 nval.bv_val = at->sat_oid;
607 nval.bv_len = strlen(at->sat_oid);
609 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
613 ldap_memfree( val.bv_val );