2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 /* attr.c - routines for dealing with attributes */
17 #include <ac/socket.h>
18 #include <ac/string.h>
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
26 #include "ldap_defaults.h"
30 static void at_index_print( void );
34 attr_free( Attribute *a )
37 ber_bvecfree( a->a_vals );
42 attrs_free( Attribute *a )
46 for( ; a != NULL ; a = next ) {
52 Attribute *attr_dup( Attribute *a )
56 if( a == NULL) return NULL;
58 tmp = ch_malloc( sizeof(Attribute) );
60 if( a->a_vals != NULL ) {
63 for( i=0; a->a_vals[i] != NULL; i++ ) {
67 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
69 for( i=0; a->a_vals[i] != NULL; i++ ) {
70 tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
72 if( tmp->a_vals[i] == NULL ) break;
75 tmp->a_vals[i] = NULL;
81 tmp->a_type = ch_strdup( a->a_type );
82 tmp->a_syntax = a->a_syntax;
88 Attribute *attrs_dup( Attribute *a )
90 Attribute *tmp, **next;
92 if( a == NULL ) return NULL;
97 for( ; a != NULL ; a = a->a_next ) {
98 *next = attr_dup( a );
99 next = &((*next)->a_next);
107 * attr_normalize - normalize an attribute name (make it all lowercase)
111 attr_normalize( char *s )
115 return( ldap_pvt_str2lower( s ) );
119 * attr_merge_fast - merge the given type and value with the list of
120 * attributes in attrs. called from str2entry(), where we can make some
121 * assumptions to make things faster.
122 * returns 0 everything went ok
130 struct berval **vals,
138 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
139 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
146 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
147 (**a)->a_type = attr_normalize( ch_strdup( type ) );
148 (**a)->a_vals = NULL;
149 (**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 (*a)->a_syntax = attr_syntax( type );
187 return( value_add( &(*a)->a_vals, vals ) );
191 * attr_find - find and return attribute type in list a
200 for ( ; a != NULL; a = a->a_next ) {
201 if ( strcasecmp( a->a_type, type ) == 0 ) {
210 * attr_delete - delete the attribute type in list pointed to by attrs
211 * return 0 deleted ok
212 * 1 not found in list a
213 * -1 something bad happened
225 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
226 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
242 #define DEFAULT_SYNTAX SYNTAX_CIS
245 * attr_syntax - return the syntax of attribute type
249 attr_syntax( char *type )
255 return( sat->sat_syntax_compat );
258 return( DEFAULT_SYNTAX );
262 * attr_syntax_config - process an attribute syntax config line
274 LDAP_ATTRIBUTE_TYPE *at;
280 Debug( LDAP_DEBUG_ANY,
281 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
286 at = (LDAP_ATTRIBUTE_TYPE *)
287 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
290 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
291 strcasecmp( argv[lasti], "cis" ) == 0 ) {
292 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
293 at->at_equality_oid = "2.5.13.2";
294 at->at_ordering_oid = "2.5.13.3";
295 at->at_substr_oid = "2.5.13.4";
296 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
297 strcasecmp( argv[lasti], "tel" ) == 0 ) {
298 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
299 at->at_equality_oid = "2.5.13.20";
300 at->at_substr_oid = "2.5.13.21";
301 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
302 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
303 at->at_equality_oid = "2.5.13.1";
304 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
305 strcasecmp( argv[lasti], "ces" ) == 0 ) {
306 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
307 at->at_equality_oid = "2.5.13.5";
308 at->at_ordering_oid = "2.5.13.6";
309 at->at_substr_oid = "2.5.13.7";
310 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
311 strcasecmp( argv[lasti], "bin" ) == 0 ) {
312 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
313 /* There is no match for binary syntax. Really */
315 Debug( LDAP_DEBUG_ANY,
316 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
317 fname, lineno, argv[lasti] );
318 Debug( LDAP_DEBUG_ANY,
319 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
321 free( (AttributeType *) at );
327 at->at_names = charray_dup( argv );
330 code = at_add( at, &err );
332 fprintf( stderr, "%s: line %d: %s %s\n",
333 fname, lineno, scherr2str(code), err);
334 exit( EXIT_FAILURE );
346 if ( at_find( name ) ) {
352 attr_syntax_config( "implicit", 0, 2, argv );
359 AttributeType *air_at;
362 static Avlnode *attr_index = NULL;
363 static AttributeType *attr_list = NULL;
367 struct aindexrec *air1,
368 struct aindexrec *air2
371 return (strcasecmp( air1->air_name, air2->air_name ));
377 struct aindexrec *air
380 return (strcasecmp( type, air->air_name ));
388 struct aindexrec *air = NULL;
390 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
391 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
392 return( air->air_at );
400 AttributeType ***listp
403 AttributeType **list;
404 AttributeType **list1;
410 list = calloc(size, sizeof(AttributeType *));
422 list1 = realloc(list, size*sizeof(AttributeType *));
437 AttributeType ***listp
440 AttributeType **list;
441 AttributeType **list1;
449 for ( i=0; list[i]; i++ )
454 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
458 /* Tell the runtime this can be shrinked */
459 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
478 for ( i=0; list[i]; i++ ) {
479 if ( sat == list[i] ) {
493 struct aindexrec *air;
497 while ( *atp != NULL ) {
498 atp = &(*atp)->sat_next;
502 if ( sat->sat_oid ) {
503 air = (struct aindexrec *)
504 ch_calloc( 1, sizeof(struct aindexrec) );
505 air->air_name = sat->sat_oid;
507 if ( avl_insert( &attr_index, (caddr_t) air,
508 (AVL_CMP) attr_index_cmp,
509 (AVL_DUP) avl_dup_error ) ) {
512 return SLAP_SCHERR_DUP_ATTR;
514 /* FIX: temporal consistency check */
515 at_find(air->air_name);
517 if ( (names = sat->sat_names) ) {
519 air = (struct aindexrec *)
520 ch_calloc( 1, sizeof(struct aindexrec) );
521 air->air_name = ch_strdup(*names);
523 if ( avl_insert( &attr_index, (caddr_t) air,
524 (AVL_CMP) attr_index_cmp,
525 (AVL_DUP) avl_dup_error ) ) {
528 return SLAP_SCHERR_DUP_ATTR;
530 /* FIX: temporal consistency check */
531 at_find(air->air_name);
541 LDAP_ATTRIBUTE_TYPE *at,
552 if ( at->at_names && at->at_names[0] ) {
553 errattr = at->at_names[0];
554 } else if ( at->at_oid ) {
555 errattr = at->at_oid;
558 return SLAP_SCHERR_ATTR_INCOMPLETE;
560 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
561 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
562 if ( at->at_sup_oid ) {
563 if ( (sat1 = at_find(at->at_sup_oid)) ) {
565 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
567 return SLAP_SCHERR_OUTOFMEM;
570 *err = at->at_sup_oid;
571 return SLAP_SCHERR_ATTR_NOT_FOUND;
575 if ( at->at_syntax_oid ) {
576 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
577 sat->sat_syntax = syn;
579 *err = sat->sat_syntax_oid;
580 return SLAP_SCHERR_SYN_NOT_FOUND;
582 if ( !strcmp(at->at_syntax_oid,
583 "1.3.6.1.4.1.1466.115.121.1.15") ) {
584 if ( at->at_equality_oid &&
585 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
586 sat->sat_syntax_compat = SYNTAX_CES;
588 sat->sat_syntax_compat = SYNTAX_CIS;
590 } else if ( !strcmp(at->at_syntax_oid,
591 "1.3.6.1.4.1.1466.115.121.1.50") ) {
592 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
593 } else if ( !strcmp(at->at_syntax_oid,
594 "1.3.6.1.4.1.1466.115.121.1.12") ) {
595 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
596 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
597 sat->sat_syntax_compat = SYNTAX_BIN;
599 sat->sat_syntax_compat = DEFAULT_SYNTAX;
602 sat->sat_syntax_compat = DEFAULT_SYNTAX;
605 if ( sat->sat_equality_oid ) {
606 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
607 sat->sat_equality = mr;
609 *err = sat->sat_equality_oid;
610 return SLAP_SCHERR_MR_NOT_FOUND;
613 if ( sat->sat_ordering_oid ) {
614 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
615 sat->sat_ordering = mr;
617 *err = sat->sat_ordering_oid;
618 return SLAP_SCHERR_MR_NOT_FOUND;
621 if ( sat->sat_substr_oid ) {
622 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
623 sat->sat_substr = mr;
625 *err = sat->sat_substr_oid;
626 return SLAP_SCHERR_MR_NOT_FOUND;
631 * Now inherit definitions from superiors. We only check the
632 * direct superior since that one has already inherited from
635 if ( sat->sat_sup ) {
636 if ( !sat->sat_syntax ) {
637 sat->sat_syntax = sat->sat_sup->sat_syntax;
638 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
640 if ( !sat->sat_equality ) {
641 sat->sat_equality = sat->sat_sup->sat_equality;
643 if ( !sat->sat_ordering ) {
644 sat->sat_ordering = sat->sat_sup->sat_ordering;
646 if ( !sat->sat_substr ) {
647 sat->sat_substr = sat->sat_sup->sat_substr;
650 code = at_insert(sat,err);
656 at_canonical_name( char * a_type )
660 if ( (atp=at_find(a_type)) == NULL ) {
664 } else if ( atp->sat_names
666 && (*(atp->sat_names[0]) != '\0') ) {
668 return atp->sat_names[0];
670 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
680 }/* char * at_canonica_name() */
683 #if defined( SLAPD_SCHEMA_DN )
686 at_schema_info( Entry *e )
689 struct berval *vals[2];
695 for ( at = attr_list; at; at = at->sat_next ) {
696 val.bv_val = ldap_attributetype2str( &at->sat_atype );
698 val.bv_len = strlen( val.bv_val );
699 Debug( LDAP_DEBUG_TRACE, "Merging at [%d] %s\n",
700 val.bv_len, val.bv_val, 0 );
701 attr_merge( e, "attributeTypes", vals );
702 ldap_memfree( val.bv_val );
714 at_index_printnode( struct aindexrec *air )
717 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
722 at_index_print( void )
724 printf("Printing attribute type index:\n");
725 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
726 0, -1, AVL_INORDER );