3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* attr.c - routines for dealing with attributes */
18 #include <ac/socket.h>
19 #include <ac/string.h>
26 static void at_index_print( void );
30 attr_free( Attribute *a )
33 ber_bvecfree( a->a_vals );
38 attrs_free( Attribute *a )
42 for( ; a != NULL ; a = next ) {
48 Attribute *attr_dup( Attribute *a )
52 if( a == NULL) return NULL;
54 tmp = ch_malloc( sizeof(Attribute) );
56 if( a->a_vals != NULL ) {
59 for( i=0; a->a_vals[i] != NULL; i++ ) {
63 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
65 for( i=0; a->a_vals[i] != NULL; i++ ) {
66 tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
68 if( tmp->a_vals[i] == NULL ) break;
71 tmp->a_vals[i] = NULL;
77 tmp->a_type = ch_strdup( a->a_type );
78 tmp->a_syntax = a->a_syntax;
84 Attribute *attrs_dup( Attribute *a )
86 Attribute *tmp, **next;
88 if( a == NULL ) return NULL;
93 for( ; a != NULL ; a = a->a_next ) {
94 *next = attr_dup( a );
95 next = &((*next)->a_next);
103 * attr_normalize - normalize an attribute name (make it all lowercase)
107 attr_normalize( char *s )
111 return( ldap_pvt_str2lower( s ) );
115 * attr_merge_fast - merge the given type and value with the list of
116 * attributes in attrs. called from str2entry(), where we can make some
117 * assumptions to make things faster.
118 * returns 0 everything went ok
126 struct berval **vals,
134 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
135 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
142 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
143 (**a)->a_type = attr_normalize( ch_strdup( type ) );
144 (**a)->a_vals = NULL;
145 (**a)->a_syntax = attr_syntax( type );
146 (**a)->a_next = NULL;
149 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
154 * attr_merge - merge the given type and value with the list of
155 * attributes in attrs.
156 * returns 0 everything went ok
169 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
170 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
176 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
177 (*a)->a_type = attr_normalize( ch_strdup( type ) );
179 (*a)->a_syntax = attr_syntax( type );
183 return( value_add( &(*a)->a_vals, vals ) );
187 * attr_find - find and return attribute type in list a
196 for ( ; a != NULL; a = a->a_next ) {
197 if ( strcasecmp( a->a_type, type ) == 0 ) {
206 * attr_delete - delete the attribute type in list pointed to by attrs
207 * return 0 deleted ok
208 * 1 not found in list a
209 * -1 something bad happened
221 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
222 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
238 #define DEFAULT_SYNTAX SYNTAX_CIS
241 * attr_syntax - return the syntax of attribute type
245 attr_syntax( char *type )
251 return( sat->sat_syntax_compat );
254 return( DEFAULT_SYNTAX );
258 * attr_syntax_config - process an attribute syntax config line
270 LDAP_ATTRIBUTE_TYPE *at;
276 Debug( LDAP_DEBUG_ANY,
277 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
282 at = (LDAP_ATTRIBUTE_TYPE *)
283 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
285 #define SYNTAX_DS_OID "1.3.6.1.4.1.1466.115.121.1.15"
286 #define SYNTAX_DSCE_OID "2.5.13.5"
287 #define SYNTAX_IA5_OID "1.3.6.1.4.1.1466.115.121.1.26"
288 #define SYNTAX_IA5CE_OID "1.3.6.1.4.1.1466.109.114.1"
289 #define SYNTAX_DN_OID "1.3.6.1.4.1.1466.115.121.1.12"
290 #define SYNTAX_TEL_OID "1.3.6.1.4.1.1466.115.121.1.50"
291 #define SYNTAX_BIN_OID "1.3.6.1.4.1.1466.115.121.1.40" /* octetString */
294 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
295 strcasecmp( argv[lasti], "cis" ) == 0 ) {
296 at->at_syntax_oid = SYNTAX_DS_OID;
297 at->at_equality_oid = "2.5.13.2";
298 at->at_ordering_oid = "2.5.13.3";
299 at->at_substr_oid = "2.5.13.4";
301 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
302 strcasecmp( argv[lasti], "tel" ) == 0 ) {
303 at->at_syntax_oid = SYNTAX_TEL_OID;
304 at->at_equality_oid = "2.5.13.20";
305 at->at_substr_oid = "2.5.13.21";
307 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
308 at->at_syntax_oid = SYNTAX_DN_OID;
309 at->at_equality_oid = "2.5.13.1";
311 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
312 strcasecmp( argv[lasti], "ces" ) == 0 ) {
313 at->at_syntax_oid = SYNTAX_DS_OID;
314 at->at_equality_oid = SYNTAX_DSCE_OID;
315 at->at_ordering_oid = "2.5.13.6";
316 at->at_substr_oid = "2.5.13.7";
318 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
319 strcasecmp( argv[lasti], "bin" ) == 0 ) {
320 /* bin -> octetString, not binary! */
321 at->at_syntax_oid = SYNTAX_BIN_OID;
322 at->at_equality_oid = "2.5.13.17";
325 Debug( LDAP_DEBUG_ANY,
326 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
327 fname, lineno, argv[lasti] );
328 Debug( LDAP_DEBUG_ANY,
329 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
331 free( (AttributeType *) at );
337 at->at_names = charray_dup( argv );
340 code = at_add( at, &err );
342 fprintf( stderr, "%s: line %d: %s %s\n",
343 fname, lineno, scherr2str(code), err);
344 exit( EXIT_FAILURE );
356 if ( at_find( name ) ) {
362 attr_syntax_config( "implicit", 0, 2, argv );
369 AttributeType *air_at;
372 static Avlnode *attr_index = NULL;
373 static AttributeType *attr_list = NULL;
377 struct aindexrec *air1,
378 struct aindexrec *air2
381 return (strcasecmp( air1->air_name, air2->air_name ));
387 struct aindexrec *air
390 return (strcasecmp( type, air->air_name ));
398 struct aindexrec *air = NULL;
399 char *p, *tmpname = NULL;
402 * The name may actually be an AttributeDescription, i.e. it may
403 * contain options. Let's deal with it.
405 p = strchr( name, ';' );
407 tmpname = ch_malloc( p-name+1 );
408 strncpy( tmpname, name, p-name );
409 tmpname[p-name] = '\0';
411 tmpname = (char *)name;
412 if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
413 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
414 if ( tmpname != name )
415 ldap_memfree( tmpname );
416 return( air->air_at );
418 if ( tmpname != name )
419 ldap_memfree( tmpname );
426 AttributeType ***listp
429 AttributeType **list;
430 AttributeType **list1;
436 list = calloc(size, sizeof(AttributeType *));
448 list1 = realloc(list, size*sizeof(AttributeType *));
463 AttributeType ***listp
466 AttributeType **list;
467 AttributeType **list1;
475 for ( i=0; list[i]; i++ )
480 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
484 /* Tell the runtime this can be shrinked */
485 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
504 for ( i=0; list[i]; i++ ) {
505 if ( sat == list[i] ) {
519 struct aindexrec *air;
523 while ( *atp != NULL ) {
524 atp = &(*atp)->sat_next;
528 if ( sat->sat_oid ) {
529 air = (struct aindexrec *)
530 ch_calloc( 1, sizeof(struct aindexrec) );
531 air->air_name = sat->sat_oid;
533 if ( avl_insert( &attr_index, (caddr_t) air,
534 (AVL_CMP) attr_index_cmp,
535 (AVL_DUP) avl_dup_error ) ) {
538 return SLAP_SCHERR_DUP_ATTR;
540 /* FIX: temporal consistency check */
541 at_find(air->air_name);
543 if ( (names = sat->sat_names) ) {
545 air = (struct aindexrec *)
546 ch_calloc( 1, sizeof(struct aindexrec) );
547 air->air_name = ch_strdup(*names);
549 if ( avl_insert( &attr_index, (caddr_t) air,
550 (AVL_CMP) attr_index_cmp,
551 (AVL_DUP) avl_dup_error ) ) {
553 ldap_memfree(air->air_name);
555 return SLAP_SCHERR_DUP_ATTR;
557 /* FIX: temporal consistency check */
558 at_find(air->air_name);
568 LDAP_ATTRIBUTE_TYPE *at,
579 if ( at->at_names && at->at_names[0] ) {
580 errattr = at->at_names[0];
581 } else if ( at->at_oid ) {
582 errattr = at->at_oid;
585 return SLAP_SCHERR_ATTR_INCOMPLETE;
587 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
588 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
590 if ( at->at_sup_oid ) {
591 if ( (sat1 = at_find(at->at_sup_oid)) ) {
593 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
595 return SLAP_SCHERR_OUTOFMEM;
598 *err = at->at_sup_oid;
599 return SLAP_SCHERR_ATTR_NOT_FOUND;
604 * Inherit definitions from superiors. We only check the
605 * direct superior since that one has already inherited from
608 if ( sat->sat_sup ) {
609 sat->sat_syntax = sat->sat_sup->sat_syntax;
611 sat->sat_equality = sat->sat_sup->sat_equality;
612 sat->sat_ordering = sat->sat_sup->sat_ordering;
613 sat->sat_substr = sat->sat_sup->sat_substr;
616 if ( at->at_syntax_oid ) {
617 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
618 sat->sat_syntax = syn;
620 *err = sat->sat_syntax_oid;
621 return SLAP_SCHERR_SYN_NOT_FOUND;
624 if ( !strcmp(at->at_syntax_oid, SYNTAX_DS_OID) ) {
625 if ( at->at_equality_oid && (
626 !strcmp(at->at_equality_oid, SYNTAX_DSCE_OID) ) )
628 sat->sat_syntax_compat = SYNTAX_CES;
630 sat->sat_syntax_compat = SYNTAX_CIS;
633 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_IA5_OID) ) {
634 if ( at->at_equality_oid && (
635 !strcmp(at->at_equality_oid, SYNTAX_IA5CE_OID) ) )
637 sat->sat_syntax_compat = SYNTAX_CES;
639 sat->sat_syntax_compat = SYNTAX_CIS;
642 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_DN_OID ) ) {
643 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
645 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_TEL_OID ) ) {
646 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
648 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_BIN_OID ) ) {
649 sat->sat_syntax_compat = SYNTAX_BIN;
652 sat->sat_syntax_compat = DEFAULT_SYNTAX;
655 } else if ( sat->sat_syntax == NULL ) {
656 return SLAP_SCHERR_ATTR_INCOMPLETE;
659 if ( sat->sat_equality_oid ) {
660 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
661 sat->sat_equality = mr;
663 *err = sat->sat_equality_oid;
664 return SLAP_SCHERR_MR_NOT_FOUND;
669 if ( sat->sat_ordering_oid ) {
670 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
671 sat->sat_ordering = mr;
673 *err = sat->sat_ordering_oid;
674 return SLAP_SCHERR_MR_NOT_FOUND;
678 if ( sat->sat_substr_oid ) {
679 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
680 sat->sat_substr = mr;
682 *err = sat->sat_substr_oid;
683 return SLAP_SCHERR_MR_NOT_FOUND;
687 code = at_insert(sat,err);
693 at_canonical_name( char * a_type )
697 if ( (atp=at_find(a_type)) == NULL ) {
701 } else if ( atp->sat_names
703 && (*(atp->sat_names[0]) != '\0') ) {
705 return atp->sat_names[0];
707 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
717 }/* char * at_canonica_name() */
720 #if defined( SLAPD_SCHEMA_DN )
723 at_schema_info( Entry *e )
726 struct berval *vals[2];
732 for ( at = attr_list; at; at = at->sat_next ) {
733 val.bv_val = ldap_attributetype2str( &at->sat_atype );
735 val.bv_len = strlen( val.bv_val );
736 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
737 (long) val.bv_len, val.bv_val, 0 );
738 attr_merge( e, "attributeTypes", vals );
739 ldap_memfree( val.bv_val );
751 at_index_printnode( struct aindexrec *air )
754 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
759 at_index_print( void )
761 printf("Printing attribute type index:\n");
762 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
763 0, -1, AVL_INORDER );