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 #ifdef SLAPD_SCHEMA_COMPAT
79 tmp->a_syntax = a->a_syntax;
86 Attribute *attrs_dup( Attribute *a )
88 Attribute *tmp, **next;
90 if( a == NULL ) return NULL;
95 for( ; a != NULL ; a = a->a_next ) {
96 *next = attr_dup( a );
97 next = &((*next)->a_next);
105 * attr_normalize - normalize an attribute name (make it all lowercase)
109 attr_normalize( char *s )
113 return( ldap_pvt_str2lower( s ) );
117 * attr_merge_fast - merge the given type and value with the list of
118 * attributes in attrs. called from str2entry(), where we can make some
119 * assumptions to make things faster.
120 * returns 0 everything went ok
128 struct berval **vals,
136 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
137 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
144 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
145 (**a)->a_type = attr_normalize( ch_strdup( type ) );
146 (**a)->a_vals = NULL;
147 #ifdef SLAPD_SCHEMA_COMPAT
148 (**a)->a_syntax = attr_syntax( type );
150 (**a)->a_next = NULL;
153 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
158 * attr_merge - merge the given type and value with the list of
159 * attributes in attrs.
160 * returns 0 everything went ok
173 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
174 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
180 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
181 (*a)->a_type = attr_normalize( ch_strdup( type ) );
183 #ifdef SLAPD_SCHEMA_COMPAT
184 (*a)->a_syntax = attr_syntax( type );
189 return( value_add( &(*a)->a_vals, vals ) );
193 * attr_find - find and return attribute type in list a
202 for ( ; a != NULL; a = a->a_next ) {
203 if ( strcasecmp( a->a_type, type ) == 0 ) {
212 * attr_delete - delete the attribute type in list pointed to by attrs
213 * return 0 deleted ok
214 * 1 not found in list a
215 * -1 something bad happened
227 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
228 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
244 #ifdef SLAPD_SCHEMA_COMPAT
246 #define DEFAULT_SYNTAX SYNTAX_CIS
249 * attr_syntax - return the syntax of attribute type
253 attr_syntax( char *type )
259 return( sat->sat_syntax_compat );
262 return( DEFAULT_SYNTAX );
266 * attr_syntax_config - process an attribute syntax config line
278 LDAP_ATTRIBUTE_TYPE *at;
284 Debug( LDAP_DEBUG_ANY,
285 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
290 at = (LDAP_ATTRIBUTE_TYPE *)
291 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
293 #define SYNTAX_DS_OID "1.3.6.1.4.1.1466.115.121.1.15"
294 #define SYNTAX_DSCE_OID "2.5.13.5"
295 #define SYNTAX_IA5_OID "1.3.6.1.4.1.1466.115.121.1.26"
296 #define SYNTAX_IA5CE_OID "1.3.6.1.4.1.1466.109.114.1"
297 #define SYNTAX_DN_OID SLAPD_OID_DN_SYNTAX
298 #define SYNTAX_TEL_OID "1.3.6.1.4.1.1466.115.121.1.50"
299 #define SYNTAX_BIN_OID "1.3.6.1.4.1.1466.115.121.1.40" /* octetString */
302 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
303 strcasecmp( argv[lasti], "cis" ) == 0 ) {
304 at->at_syntax_oid = SYNTAX_DS_OID;
305 at->at_equality_oid = "2.5.13.2";
306 at->at_ordering_oid = "2.5.13.3";
307 at->at_substr_oid = "2.5.13.4";
309 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
310 strcasecmp( argv[lasti], "tel" ) == 0 ) {
311 at->at_syntax_oid = SYNTAX_TEL_OID;
312 at->at_equality_oid = "2.5.13.20";
313 at->at_substr_oid = "2.5.13.21";
315 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
316 at->at_syntax_oid = SYNTAX_DN_OID;
317 at->at_equality_oid = "2.5.13.1";
319 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
320 strcasecmp( argv[lasti], "ces" ) == 0 ) {
321 at->at_syntax_oid = SYNTAX_DS_OID;
322 at->at_equality_oid = SYNTAX_DSCE_OID;
323 at->at_ordering_oid = "2.5.13.6";
324 at->at_substr_oid = "2.5.13.7";
326 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
327 strcasecmp( argv[lasti], "bin" ) == 0 ) {
328 /* bin -> octetString, not binary! */
329 at->at_syntax_oid = SYNTAX_BIN_OID;
330 at->at_equality_oid = "2.5.13.17";
333 Debug( LDAP_DEBUG_ANY,
334 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
335 fname, lineno, argv[lasti] );
336 Debug( LDAP_DEBUG_ANY,
337 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
339 free( (AttributeType *) at );
345 at->at_names = charray_dup( argv );
348 code = at_add( at, &err );
350 fprintf( stderr, "%s: line %d: %s %s\n",
351 fname, lineno, scherr2str(code), err);
352 exit( EXIT_FAILURE );
365 if ( at_find( name ) ) {
371 attr_syntax_config( "implicit", 0, 2, argv );
379 AttributeType *air_at;
382 static Avlnode *attr_index = NULL;
383 static AttributeType *attr_list = NULL;
387 struct aindexrec *air1,
388 struct aindexrec *air2
391 return (strcasecmp( air1->air_name, air2->air_name ));
397 struct aindexrec *air
400 return (strcasecmp( type, air->air_name ));
408 struct aindexrec *air = NULL;
409 char *p, *tmpname = NULL;
412 * The name may actually be an AttributeDescription, i.e. it may
413 * contain options. Let's deal with it.
415 p = strchr( name, ';' );
417 tmpname = ch_malloc( p-name+1 );
418 strncpy( tmpname, name, p-name );
419 tmpname[p-name] = '\0';
421 tmpname = (char *)name;
424 if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
425 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
426 if ( tmpname != name )
427 ldap_memfree( tmpname );
428 return( air->air_at );
431 if ( tmpname != name )
432 ldap_memfree( tmpname );
439 AttributeType ***listp
442 AttributeType **list;
443 AttributeType **list1;
449 list = calloc(size, sizeof(AttributeType *));
461 list1 = realloc(list, size*sizeof(AttributeType *));
476 AttributeType ***listp
479 AttributeType **list;
480 AttributeType **list1;
488 for ( i=0; list[i]; i++ )
493 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
497 /* Tell the runtime this can be shrinked */
498 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
517 for ( i=0; list[i]; i++ ) {
518 if ( sat == list[i] ) {
532 struct aindexrec *air;
536 while ( *atp != NULL ) {
537 atp = &(*atp)->sat_next;
541 if ( sat->sat_oid ) {
542 air = (struct aindexrec *)
543 ch_calloc( 1, sizeof(struct aindexrec) );
544 air->air_name = sat->sat_oid;
546 if ( avl_insert( &attr_index, (caddr_t) air,
547 (AVL_CMP) attr_index_cmp,
548 (AVL_DUP) avl_dup_error ) ) {
551 return SLAP_SCHERR_DUP_ATTR;
553 /* FIX: temporal consistency check */
554 at_find(air->air_name);
556 if ( (names = sat->sat_names) ) {
558 air = (struct aindexrec *)
559 ch_calloc( 1, sizeof(struct aindexrec) );
560 air->air_name = ch_strdup(*names);
562 if ( avl_insert( &attr_index, (caddr_t) air,
563 (AVL_CMP) attr_index_cmp,
564 (AVL_DUP) avl_dup_error ) ) {
566 ldap_memfree(air->air_name);
568 return SLAP_SCHERR_DUP_ATTR;
570 /* FIX: temporal consistency check */
571 at_find(air->air_name);
581 LDAP_ATTRIBUTE_TYPE *at,
592 if ( at->at_names && at->at_names[0] ) {
593 errattr = at->at_names[0];
594 } else if ( at->at_oid ) {
595 errattr = at->at_oid;
598 return SLAP_SCHERR_ATTR_INCOMPLETE;
600 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
601 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
603 if ( at->at_sup_oid ) {
604 if ( (sat1 = at_find(at->at_sup_oid)) ) {
606 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
608 return SLAP_SCHERR_OUTOFMEM;
611 *err = at->at_sup_oid;
612 return SLAP_SCHERR_ATTR_NOT_FOUND;
617 * Inherit definitions from superiors. We only check the
618 * direct superior since that one has already inherited from
621 if ( sat->sat_sup ) {
622 sat->sat_syntax = sat->sat_sup->sat_syntax;
624 sat->sat_equality = sat->sat_sup->sat_equality;
625 sat->sat_ordering = sat->sat_sup->sat_ordering;
626 sat->sat_substr = sat->sat_sup->sat_substr;
629 if ( at->at_syntax_oid ) {
630 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
631 sat->sat_syntax = syn;
633 *err = sat->sat_syntax_oid;
634 return SLAP_SCHERR_SYN_NOT_FOUND;
637 #ifdef SLAPD_SCHEMA_COMPAT
638 if ( !strcmp(at->at_syntax_oid, SYNTAX_DS_OID) ) {
639 if ( at->at_equality_oid && (
640 !strcmp(at->at_equality_oid, SYNTAX_DSCE_OID) ) )
642 sat->sat_syntax_compat = SYNTAX_CES;
644 sat->sat_syntax_compat = SYNTAX_CIS;
647 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_IA5_OID) ) {
648 if ( at->at_equality_oid && (
649 !strcmp(at->at_equality_oid, SYNTAX_IA5CE_OID) ) )
651 sat->sat_syntax_compat = SYNTAX_CES;
653 sat->sat_syntax_compat = SYNTAX_CIS;
656 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_DN_OID ) ) {
657 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
659 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_TEL_OID ) ) {
660 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
662 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_BIN_OID ) ) {
663 sat->sat_syntax_compat = SYNTAX_BIN;
666 sat->sat_syntax_compat = DEFAULT_SYNTAX;
670 } else if ( sat->sat_syntax == NULL ) {
671 return SLAP_SCHERR_ATTR_INCOMPLETE;
674 if ( sat->sat_equality_oid ) {
675 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
676 sat->sat_equality = mr;
678 *err = sat->sat_equality_oid;
679 return SLAP_SCHERR_MR_NOT_FOUND;
684 if ( sat->sat_ordering_oid ) {
685 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
686 sat->sat_ordering = mr;
688 *err = sat->sat_ordering_oid;
689 return SLAP_SCHERR_MR_NOT_FOUND;
693 if ( sat->sat_substr_oid ) {
694 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
695 sat->sat_substr = mr;
697 *err = sat->sat_substr_oid;
698 return SLAP_SCHERR_MR_NOT_FOUND;
702 code = at_insert(sat,err);
708 #ifdef SLAPD_SCHEMA_COMPAT
709 at_canonical_name( char * a_type )
711 at_canonical_name( AttributeType * atp )
714 #ifdef SLAPD_SCHEMA_COMPAT
721 #ifdef SLAPD_SCHEMA_COMPAT
727 } else if ( atp->sat_names
728 && atp->sat_names[0] && (*(atp->sat_names[0]) != '\0') )
730 return atp->sat_names[0];
732 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
736 #ifdef SLAPD_SCHEMA_COMPAT
743 #if defined( SLAPD_SCHEMA_DN )
745 at_schema_info( Entry *e )
748 struct berval *vals[2];
754 for ( at = attr_list; at; at = at->sat_next ) {
755 val.bv_val = ldap_attributetype2str( &at->sat_atype );
757 val.bv_len = strlen( val.bv_val );
758 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
759 (long) val.bv_len, val.bv_val, 0 );
760 attr_merge( e, "attributeTypes", vals );
761 ldap_memfree( val.bv_val );
772 at_index_printnode( struct aindexrec *air )
777 ldap_attributetype2str(&air->air_at->sat_atype) );
782 at_index_print( void )
784 printf("Printing attribute type index:\n");
785 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
786 0, -1, AVL_INORDER );