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>
22 #ifdef HAVE_SYS_PARAM_H
23 #include <sys/param.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;
389 char *p, *tmpname = NULL;
392 * The name may actually be an AttributeDescription, i.e. it may
393 * contain options. Let's deal with it.
395 p = strchr( name, ';' );
397 tmpname = ch_malloc( p-name+1 );
398 strncpy( tmpname, name, p-name );
399 tmpname[p-name] = '\0';
401 tmpname = (char *)name;
402 if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
403 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
404 if ( tmpname != name )
405 ldap_memfree( tmpname );
406 return( air->air_at );
408 if ( tmpname != name )
409 ldap_memfree( tmpname );
416 AttributeType ***listp
419 AttributeType **list;
420 AttributeType **list1;
426 list = calloc(size, sizeof(AttributeType *));
438 list1 = realloc(list, size*sizeof(AttributeType *));
453 AttributeType ***listp
456 AttributeType **list;
457 AttributeType **list1;
465 for ( i=0; list[i]; i++ )
470 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
474 /* Tell the runtime this can be shrinked */
475 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
494 for ( i=0; list[i]; i++ ) {
495 if ( sat == list[i] ) {
509 struct aindexrec *air;
513 while ( *atp != NULL ) {
514 atp = &(*atp)->sat_next;
518 if ( sat->sat_oid ) {
519 air = (struct aindexrec *)
520 ch_calloc( 1, sizeof(struct aindexrec) );
521 air->air_name = sat->sat_oid;
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);
533 if ( (names = sat->sat_names) ) {
535 air = (struct aindexrec *)
536 ch_calloc( 1, sizeof(struct aindexrec) );
537 air->air_name = ch_strdup(*names);
539 if ( avl_insert( &attr_index, (caddr_t) air,
540 (AVL_CMP) attr_index_cmp,
541 (AVL_DUP) avl_dup_error ) ) {
544 return SLAP_SCHERR_DUP_ATTR;
546 /* FIX: temporal consistency check */
547 at_find(air->air_name);
557 LDAP_ATTRIBUTE_TYPE *at,
568 if ( at->at_names && at->at_names[0] ) {
569 errattr = at->at_names[0];
570 } else if ( at->at_oid ) {
571 errattr = at->at_oid;
574 return SLAP_SCHERR_ATTR_INCOMPLETE;
576 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
577 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
578 if ( at->at_sup_oid ) {
579 if ( (sat1 = at_find(at->at_sup_oid)) ) {
581 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
583 return SLAP_SCHERR_OUTOFMEM;
586 *err = at->at_sup_oid;
587 return SLAP_SCHERR_ATTR_NOT_FOUND;
591 if ( at->at_syntax_oid ) {
592 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
593 sat->sat_syntax = syn;
595 *err = sat->sat_syntax_oid;
596 return SLAP_SCHERR_SYN_NOT_FOUND;
598 if ( !strcmp(at->at_syntax_oid,
599 "1.3.6.1.4.1.1466.115.121.1.15") ) {
600 if ( at->at_equality_oid &&
601 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
602 sat->sat_syntax_compat = SYNTAX_CES;
604 sat->sat_syntax_compat = SYNTAX_CIS;
606 } else if ( !strcmp(at->at_syntax_oid,
607 "1.3.6.1.4.1.1466.115.121.1.50") ) {
608 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
609 } else if ( !strcmp(at->at_syntax_oid,
610 "1.3.6.1.4.1.1466.115.121.1.12") ) {
611 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
612 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
613 sat->sat_syntax_compat = SYNTAX_BIN;
615 sat->sat_syntax_compat = DEFAULT_SYNTAX;
618 sat->sat_syntax_compat = DEFAULT_SYNTAX;
621 if ( sat->sat_equality_oid ) {
622 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
623 sat->sat_equality = mr;
625 *err = sat->sat_equality_oid;
626 return SLAP_SCHERR_MR_NOT_FOUND;
629 if ( sat->sat_ordering_oid ) {
630 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
631 sat->sat_ordering = mr;
633 *err = sat->sat_ordering_oid;
634 return SLAP_SCHERR_MR_NOT_FOUND;
637 if ( sat->sat_substr_oid ) {
638 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
639 sat->sat_substr = mr;
641 *err = sat->sat_substr_oid;
642 return SLAP_SCHERR_MR_NOT_FOUND;
647 * Now inherit definitions from superiors. We only check the
648 * direct superior since that one has already inherited from
651 if ( sat->sat_sup ) {
652 if ( !sat->sat_syntax ) {
653 sat->sat_syntax = sat->sat_sup->sat_syntax;
654 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
656 if ( !sat->sat_equality ) {
657 sat->sat_equality = sat->sat_sup->sat_equality;
659 if ( !sat->sat_ordering ) {
660 sat->sat_ordering = sat->sat_sup->sat_ordering;
662 if ( !sat->sat_substr ) {
663 sat->sat_substr = sat->sat_sup->sat_substr;
666 code = at_insert(sat,err);
672 at_canonical_name( char * a_type )
676 if ( (atp=at_find(a_type)) == NULL ) {
680 } else if ( atp->sat_names
682 && (*(atp->sat_names[0]) != '\0') ) {
684 return atp->sat_names[0];
686 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
696 }/* char * at_canonica_name() */
699 #if defined( SLAPD_SCHEMA_DN )
702 at_schema_info( Entry *e )
705 struct berval *vals[2];
711 for ( at = attr_list; at; at = at->sat_next ) {
712 val.bv_val = ldap_attributetype2str( &at->sat_atype );
714 val.bv_len = strlen( val.bv_val );
715 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
716 (long) val.bv_len, val.bv_val, 0 );
717 attr_merge( e, "attributeTypes", vals );
718 ldap_memfree( val.bv_val );
730 at_index_printnode( struct aindexrec *air )
733 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
738 at_index_print( void )
740 printf("Printing attribute type index:\n");
741 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
742 0, -1, AVL_INORDER );