3 * Copyright 1998-2002 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 AttributeType *attr_list = NULL;
56 struct aindexrec *air1,
57 struct aindexrec *air2
60 int i = air1->air_name.bv_len - air2->air_name.bv_len;
63 return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
72 int i = type->bv_len - air->air_name.bv_len;
75 return (strncasecmp( type->bv_val, air->air_name.bv_val,
86 bv.bv_val = (char *)name;
87 bv.bv_len = strlen( name );
89 return at_bvfind( &bv );
97 struct aindexrec *air;
99 air = (struct aindexrec *) avl_find( attr_index, name,
100 (AVL_CMP) attr_index_name_cmp );
102 return air != NULL ? air->air_at : NULL;
108 AttributeType ***listp
111 AttributeType **list;
112 AttributeType **list1;
118 list = ch_calloc(size, sizeof(AttributeType *));
130 list1 = ch_realloc(list, size*sizeof(AttributeType *));
145 AttributeType ***listp
148 AttributeType **list;
149 AttributeType **list1;
157 for ( i=0; list[i]; i++ )
162 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
166 /* Tell the runtime this can be shrinked */
167 list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
186 for ( i=0; list[i]; i++ ) {
187 if ( sat == list[i] ) {
197 AttributeType *a, *n;
198 avl_free(attr_index, ldap_memfree);
200 for (a=attr_list; a; a=n) {
202 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
203 ad_destroy(a->sat_ad);
204 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
205 ldap_attributetype_free((LDAPAttributeType *)a);
207 if ( slap_schema.si_at_undefined )
208 ad_destroy(slap_schema.si_at_undefined->sat_ad);
212 at_start( AttributeType **at )
218 return (*at != NULL);
222 at_next( AttributeType **at )
226 #if 1 /* pedantic check */
230 for ( tmp = attr_list; tmp; tmp = tmp->sat_next ) {
240 *at = (*at)->sat_next;
242 return (*at != NULL);
254 struct aindexrec *air;
258 while ( *atp != NULL ) {
259 atp = &(*atp)->sat_next;
263 if ( sat->sat_oid ) {
264 air = (struct aindexrec *)
265 ch_calloc( 1, sizeof(struct aindexrec) );
266 air->air_name.bv_val = sat->sat_oid;
267 air->air_name.bv_len = strlen(sat->sat_oid);
269 if ( avl_insert( &attr_index, (caddr_t) air,
270 (AVL_CMP) attr_index_cmp,
271 (AVL_DUP) avl_dup_error ) ) {
274 return SLAP_SCHERR_ATTR_DUP;
276 /* FIX: temporal consistency check */
277 at_bvfind(&air->air_name);
280 if ( (names = sat->sat_names) ) {
282 air = (struct aindexrec *)
283 ch_calloc( 1, sizeof(struct aindexrec) );
284 air->air_name.bv_val = *names;
285 air->air_name.bv_len = strlen(*names);
287 if ( avl_insert( &attr_index, (caddr_t) air,
288 (AVL_CMP) attr_index_cmp,
289 (AVL_DUP) avl_dup_error ) ) {
292 return SLAP_SCHERR_ATTR_DUP;
294 /* FIX: temporal consistency check */
295 at_bvfind(&air->air_name);
305 LDAPAttributeType *at,
317 if ( !OID_LEADCHAR( at->at_oid[0] )) {
318 /* Expand OID macros */
319 oid = oidm_find( at->at_oid );
322 return SLAP_SCHERR_OIDM;
324 if ( oid != at->at_oid ) {
325 ldap_memfree( at->at_oid );
330 if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
331 /* Expand OID macros */
332 oid = oidm_find( at->at_syntax_oid );
334 *err = at->at_syntax_oid;
335 return SLAP_SCHERR_OIDM;
337 if ( oid != at->at_syntax_oid ) {
338 ldap_memfree( at->at_syntax_oid );
339 at->at_syntax_oid = oid;
343 if ( at->at_names && at->at_names[0] ) {
346 for( i=0; at->at_names[i]; i++ ) {
347 if( !slap_valid_descr( at->at_names[i] ) ) {
348 *err = at->at_names[i];
349 return SLAP_SCHERR_BAD_DESCR;
353 cname = at->at_names[0];
355 } else if ( at->at_oid ) {
360 return SLAP_SCHERR_ATTR_INCOMPLETE;
365 if ( !at->at_usage && at->at_no_user_mod ) {
366 /* user attribute must be modifable */
367 return SLAP_SCHERR_ATTR_BAD_USAGE;
370 if ( at->at_collective ) {
372 /* collective attributes cannot be operational */
373 return SLAP_SCHERR_ATTR_BAD_USAGE;
376 if( at->at_single_value ) {
377 /* collective attributes cannot be single-valued */
378 return SLAP_SCHERR_ATTR_BAD_USAGE;
382 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
383 AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
385 sat->sat_cname.bv_val = cname;
386 sat->sat_cname.bv_len = strlen( cname );
387 ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
389 if ( at->at_sup_oid ) {
390 AttributeType *supsat = at_find(at->at_sup_oid);
392 if ( supsat == NULL ) {
393 *err = at->at_sup_oid;
394 return SLAP_SCHERR_ATTR_NOT_FOUND;
397 sat->sat_sup = supsat;
399 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
400 return SLAP_SCHERR_OUTOFMEM;
403 if ( sat->sat_usage != supsat->sat_usage ) {
404 /* subtypes must have same usage as their SUP */
405 return SLAP_SCHERR_ATTR_BAD_USAGE;
408 if ( sat->sat_flags & SLAP_AT_FINAL ) {
409 /* cannot subtype a "final" attribute type */
410 return SLAP_SCHERR_ATTR_BAD_SUP;
415 * Inherit definitions from superiors. We only check the
416 * direct superior since that one has already inherited from
419 if ( sat->sat_sup ) {
420 sat->sat_syntax = sat->sat_sup->sat_syntax;
421 sat->sat_equality = sat->sat_sup->sat_equality;
422 sat->sat_approx = sat->sat_sup->sat_approx;
423 sat->sat_ordering = sat->sat_sup->sat_ordering;
424 sat->sat_substr = sat->sat_sup->sat_substr;
427 if ( at->at_syntax_oid ) {
428 syn = syn_find(sat->sat_syntax_oid);
430 *err = sat->sat_syntax_oid;
431 return SLAP_SCHERR_SYN_NOT_FOUND;
434 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
435 return SLAP_SCHERR_ATTR_BAD_SUP;
438 sat->sat_syntax = syn;
440 } else if ( sat->sat_syntax == NULL ) {
441 return SLAP_SCHERR_ATTR_INCOMPLETE;
444 if ( sat->sat_equality_oid ) {
445 mr = mr_find(sat->sat_equality_oid);
448 *err = sat->sat_equality_oid;
449 return SLAP_SCHERR_MR_NOT_FOUND;
452 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
453 *err = sat->sat_equality_oid;
454 return SLAP_SCHERR_ATTR_BAD_MR;
457 if( sat->sat_syntax != mr->smr_syntax ) {
458 if( mr->smr_compat_syntaxes == NULL ) {
459 *err = sat->sat_equality_oid;
460 return SLAP_SCHERR_ATTR_BAD_MR;
463 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
464 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
471 *err = sat->sat_equality_oid;
472 return SLAP_SCHERR_ATTR_BAD_MR;
476 sat->sat_equality = mr;
477 sat->sat_approx = mr->smr_associated;
480 if ( sat->sat_ordering_oid ) {
481 mr = mr_find(sat->sat_ordering_oid);
484 *err = sat->sat_ordering_oid;
485 return SLAP_SCHERR_MR_NOT_FOUND;
488 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
489 *err = sat->sat_ordering_oid;
490 return SLAP_SCHERR_ATTR_BAD_MR;
493 if( sat->sat_syntax != mr->smr_syntax ) {
494 if( mr->smr_compat_syntaxes == NULL ) {
495 *err = sat->sat_ordering_oid;
496 return SLAP_SCHERR_ATTR_BAD_MR;
499 for(i=0; mr->smr_compat_syntaxes[i]; i++) {
500 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
507 *err = sat->sat_ordering_oid;
508 return SLAP_SCHERR_ATTR_BAD_MR;
512 sat->sat_ordering = mr;
515 if ( sat->sat_substr_oid ) {
516 mr = mr_find(sat->sat_substr_oid);
519 *err = sat->sat_substr_oid;
520 return SLAP_SCHERR_MR_NOT_FOUND;
523 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
524 *err = sat->sat_substr_oid;
525 return SLAP_SCHERR_ATTR_BAD_MR;
528 /* due to funky LDAP builtin substring rules, we
529 * we check against the equality rule assertion
530 * syntax and compat syntaxes instead of those
531 * associated with the substrings rule.
533 if( sat->sat_equality &&
534 sat->sat_syntax != sat->sat_equality->smr_syntax )
536 if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
537 *err = sat->sat_substr_oid;
538 return SLAP_SCHERR_ATTR_BAD_MR;
541 for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
542 if( sat->sat_syntax ==
543 sat->sat_equality->smr_compat_syntaxes[i] )
551 *err = sat->sat_substr_oid;
552 return SLAP_SCHERR_ATTR_BAD_MR;
556 sat->sat_substr = mr;
559 code = at_insert(sat,err);
565 at_index_printnode( struct aindexrec *air )
569 air->air_name.bv_val,
570 ldap_attributetype2str(&air->air_at->sat_atype) );
575 at_index_print( void )
577 printf("Printing attribute type index:\n");
578 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
579 0, -1, AVL_INORDER );
584 at_schema_info( Entry *e )
586 struct berval vals[2];
589 AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
591 vals[1].bv_val = NULL;
593 for ( at = attr_list; at; at = at->sat_next ) {
594 if( at->sat_flags & SLAP_AT_HIDE ) continue;
596 if ( ldap_attributetype2bv( &at->sat_atype, vals ) == NULL ) {
600 attr_merge( e, ad_attributeTypes, vals );
601 ldap_memfree( vals[0].bv_val );