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) );
286 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
287 strcasecmp( argv[lasti], "cis" ) == 0 ) {
288 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
289 at->at_equality_oid = "2.5.13.2";
290 at->at_ordering_oid = "2.5.13.3";
291 at->at_substr_oid = "2.5.13.4";
292 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
293 strcasecmp( argv[lasti], "tel" ) == 0 ) {
294 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
295 at->at_equality_oid = "2.5.13.20";
296 at->at_substr_oid = "2.5.13.21";
297 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
298 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
299 at->at_equality_oid = "2.5.13.1";
300 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
301 strcasecmp( argv[lasti], "ces" ) == 0 ) {
302 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
303 at->at_equality_oid = "2.5.13.5";
304 at->at_ordering_oid = "2.5.13.6";
305 at->at_substr_oid = "2.5.13.7";
306 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
307 strcasecmp( argv[lasti], "bin" ) == 0 ) {
308 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
309 /* There is no match for binary syntax. Really */
311 Debug( LDAP_DEBUG_ANY,
312 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
313 fname, lineno, argv[lasti] );
314 Debug( LDAP_DEBUG_ANY,
315 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
317 free( (AttributeType *) at );
323 at->at_names = charray_dup( argv );
326 code = at_add( at, &err );
328 fprintf( stderr, "%s: line %d: %s %s\n",
329 fname, lineno, scherr2str(code), err);
330 exit( EXIT_FAILURE );
342 if ( at_find( name ) ) {
348 attr_syntax_config( "implicit", 0, 2, argv );
355 AttributeType *air_at;
358 static Avlnode *attr_index = NULL;
359 static AttributeType *attr_list = NULL;
363 struct aindexrec *air1,
364 struct aindexrec *air2
367 return (strcasecmp( air1->air_name, air2->air_name ));
373 struct aindexrec *air
376 return (strcasecmp( type, air->air_name ));
384 struct aindexrec *air = NULL;
385 char *p, *tmpname = NULL;
388 * The name may actually be an AttributeDescription, i.e. it may
389 * contain options. Let's deal with it.
391 p = strchr( name, ';' );
393 tmpname = ch_malloc( p-name+1 );
394 strncpy( tmpname, name, p-name );
395 tmpname[p-name] = '\0';
397 tmpname = (char *)name;
398 if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
399 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
400 if ( tmpname != name )
401 ldap_memfree( tmpname );
402 return( air->air_at );
404 if ( tmpname != name )
405 ldap_memfree( tmpname );
412 AttributeType ***listp
415 AttributeType **list;
416 AttributeType **list1;
422 list = calloc(size, sizeof(AttributeType *));
434 list1 = realloc(list, size*sizeof(AttributeType *));
449 AttributeType ***listp
452 AttributeType **list;
453 AttributeType **list1;
461 for ( i=0; list[i]; i++ )
466 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
470 /* Tell the runtime this can be shrinked */
471 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
490 for ( i=0; list[i]; i++ ) {
491 if ( sat == list[i] ) {
505 struct aindexrec *air;
509 while ( *atp != NULL ) {
510 atp = &(*atp)->sat_next;
514 if ( sat->sat_oid ) {
515 air = (struct aindexrec *)
516 ch_calloc( 1, sizeof(struct aindexrec) );
517 air->air_name = sat->sat_oid;
519 if ( avl_insert( &attr_index, (caddr_t) air,
520 (AVL_CMP) attr_index_cmp,
521 (AVL_DUP) avl_dup_error ) ) {
524 return SLAP_SCHERR_DUP_ATTR;
526 /* FIX: temporal consistency check */
527 at_find(air->air_name);
529 if ( (names = sat->sat_names) ) {
531 air = (struct aindexrec *)
532 ch_calloc( 1, sizeof(struct aindexrec) );
533 air->air_name = ch_strdup(*names);
535 if ( avl_insert( &attr_index, (caddr_t) air,
536 (AVL_CMP) attr_index_cmp,
537 (AVL_DUP) avl_dup_error ) ) {
539 ldap_memfree(air->air_name);
541 return SLAP_SCHERR_DUP_ATTR;
543 /* FIX: temporal consistency check */
544 at_find(air->air_name);
554 LDAP_ATTRIBUTE_TYPE *at,
565 if ( at->at_names && at->at_names[0] ) {
566 errattr = at->at_names[0];
567 } else if ( at->at_oid ) {
568 errattr = at->at_oid;
571 return SLAP_SCHERR_ATTR_INCOMPLETE;
573 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
574 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
575 if ( at->at_sup_oid ) {
576 if ( (sat1 = at_find(at->at_sup_oid)) ) {
578 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
580 return SLAP_SCHERR_OUTOFMEM;
583 *err = at->at_sup_oid;
584 return SLAP_SCHERR_ATTR_NOT_FOUND;
588 if ( at->at_syntax_oid ) {
589 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
590 sat->sat_syntax = syn;
592 *err = sat->sat_syntax_oid;
593 return SLAP_SCHERR_SYN_NOT_FOUND;
595 if ( !strcmp(at->at_syntax_oid,
596 "1.3.6.1.4.1.1466.115.121.1.15") ) {
597 if ( at->at_equality_oid &&
598 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
599 sat->sat_syntax_compat = SYNTAX_CES;
601 sat->sat_syntax_compat = SYNTAX_CIS;
603 } else if ( !strcmp(at->at_syntax_oid,
604 "1.3.6.1.4.1.1466.115.121.1.50") ) {
605 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
606 } else if ( !strcmp(at->at_syntax_oid,
607 "1.3.6.1.4.1.1466.115.121.1.12") ) {
608 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
609 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
610 sat->sat_syntax_compat = SYNTAX_BIN;
612 sat->sat_syntax_compat = DEFAULT_SYNTAX;
615 sat->sat_syntax_compat = DEFAULT_SYNTAX;
618 if ( sat->sat_equality_oid ) {
619 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
620 sat->sat_equality = mr;
622 *err = sat->sat_equality_oid;
623 return SLAP_SCHERR_MR_NOT_FOUND;
626 if ( sat->sat_ordering_oid ) {
627 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
628 sat->sat_ordering = mr;
630 *err = sat->sat_ordering_oid;
631 return SLAP_SCHERR_MR_NOT_FOUND;
634 if ( sat->sat_substr_oid ) {
635 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
636 sat->sat_substr = mr;
638 *err = sat->sat_substr_oid;
639 return SLAP_SCHERR_MR_NOT_FOUND;
644 * Now inherit definitions from superiors. We only check the
645 * direct superior since that one has already inherited from
648 if ( sat->sat_sup ) {
649 if ( !sat->sat_syntax ) {
650 sat->sat_syntax = sat->sat_sup->sat_syntax;
651 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
653 if ( !sat->sat_equality ) {
654 sat->sat_equality = sat->sat_sup->sat_equality;
656 if ( !sat->sat_ordering ) {
657 sat->sat_ordering = sat->sat_sup->sat_ordering;
659 if ( !sat->sat_substr ) {
660 sat->sat_substr = sat->sat_sup->sat_substr;
663 code = at_insert(sat,err);
669 at_canonical_name( char * a_type )
673 if ( (atp=at_find(a_type)) == NULL ) {
677 } else if ( atp->sat_names
679 && (*(atp->sat_names[0]) != '\0') ) {
681 return atp->sat_names[0];
683 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
693 }/* char * at_canonica_name() */
696 #if defined( SLAPD_SCHEMA_DN )
699 at_schema_info( Entry *e )
702 struct berval *vals[2];
708 for ( at = attr_list; at; at = at->sat_next ) {
709 val.bv_val = ldap_attributetype2str( &at->sat_atype );
711 val.bv_len = strlen( val.bv_val );
712 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
713 (long) val.bv_len, val.bv_val, 0 );
714 attr_merge( e, "attributeTypes", vals );
715 ldap_memfree( val.bv_val );
727 at_index_printnode( struct aindexrec *air )
730 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
735 at_index_print( void )
737 printf("Printing attribute type index:\n");
738 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
739 0, -1, AVL_INORDER );