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_NOT_COMPAT
81 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 #ifdef SLAPD_SCHEMA_COMPAT
150 (**a)->a_syntax = attr_syntax( type );
152 (**a)->a_next = NULL;
155 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
160 * attr_merge - merge the given type and value with the list of
161 * attributes in attrs.
162 * returns 0 everything went ok
175 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
176 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
182 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
183 (*a)->a_type = attr_normalize( ch_strdup( type ) );
185 #ifdef SLAPD_SCHEMA_COMPAT
186 (*a)->a_syntax = attr_syntax( type );
191 return( value_add( &(*a)->a_vals, vals ) );
195 * attr_find - find and return attribute type in list a
204 for ( ; a != NULL; a = a->a_next ) {
205 if ( strcasecmp( a->a_type, type ) == 0 ) {
214 * attr_delete - delete the attribute type in list pointed to by attrs
215 * return 0 deleted ok
216 * 1 not found in list a
217 * -1 something bad happened
229 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
230 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
246 #ifdef SLAPD_SCHEMA_COMPAT
248 #define DEFAULT_SYNTAX SYNTAX_CIS
251 * attr_syntax - return the syntax of attribute type
255 attr_syntax( const char *type )
261 return( sat->sat_syntax_compat );
264 return( DEFAULT_SYNTAX );
268 * attr_syntax_config - process an attribute syntax config line
280 LDAP_ATTRIBUTE_TYPE *at;
286 Debug( LDAP_DEBUG_ANY,
287 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
292 at = (LDAP_ATTRIBUTE_TYPE *)
293 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
295 #define SYNTAX_DS_OID "1.3.6.1.4.1.1466.115.121.1.15"
296 #define SYNTAX_DSCE_OID "2.5.13.5"
297 #define SYNTAX_IA5_OID "1.3.6.1.4.1.1466.115.121.1.26"
298 #define SYNTAX_IA5CE_OID "1.3.6.1.4.1.1466.109.114.1"
299 #define SYNTAX_DN_OID SLAPD_OID_DN_SYNTAX
300 #define SYNTAX_TEL_OID "1.3.6.1.4.1.1466.115.121.1.50"
301 #define SYNTAX_BIN_OID "1.3.6.1.4.1.1466.115.121.1.40" /* octetString */
304 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
305 strcasecmp( argv[lasti], "cis" ) == 0 ) {
306 at->at_syntax_oid = SYNTAX_DS_OID;
307 at->at_equality_oid = "2.5.13.2";
308 at->at_ordering_oid = "2.5.13.3";
309 at->at_substr_oid = "2.5.13.4";
311 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
312 strcasecmp( argv[lasti], "tel" ) == 0 ) {
313 at->at_syntax_oid = SYNTAX_TEL_OID;
314 at->at_equality_oid = "2.5.13.20";
315 at->at_substr_oid = "2.5.13.21";
317 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
318 at->at_syntax_oid = SYNTAX_DN_OID;
319 at->at_equality_oid = "2.5.13.1";
321 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
322 strcasecmp( argv[lasti], "ces" ) == 0 ) {
323 at->at_syntax_oid = SYNTAX_DS_OID;
324 at->at_equality_oid = SYNTAX_DSCE_OID;
325 at->at_ordering_oid = "2.5.13.6";
326 at->at_substr_oid = "2.5.13.7";
328 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
329 strcasecmp( argv[lasti], "bin" ) == 0 ) {
330 /* bin -> octetString, not binary! */
331 at->at_syntax_oid = SYNTAX_BIN_OID;
332 at->at_equality_oid = "2.5.13.17";
335 Debug( LDAP_DEBUG_ANY,
336 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
337 fname, lineno, argv[lasti] );
338 Debug( LDAP_DEBUG_ANY,
339 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
341 free( (AttributeType *) at );
347 at->at_names = charray_dup( argv );
350 code = at_add( at, &err );
352 fprintf( stderr, "%s: line %d: %s %s\n",
353 fname, lineno, scherr2str(code), err);
354 exit( EXIT_FAILURE );
367 if ( at_find( name ) ) {
370 argv[0] = (char*) name;
373 attr_syntax_config( "implicit", 0, 2, argv );
381 AttributeType *air_at;
384 static Avlnode *attr_index = NULL;
385 static AttributeType *attr_list = NULL;
389 struct aindexrec *air1,
390 struct aindexrec *air2
393 return (strcasecmp( air1->air_name, air2->air_name ));
399 struct aindexrec *air
402 return (strcasecmp( type, air->air_name ));
410 struct aindexrec *air = NULL;
411 char *p, *tmpname = NULL;
414 * The name may actually be an AttributeDescription, i.e. it may
415 * contain options. Let's deal with it.
417 p = strchr( name, ';' );
419 tmpname = ch_malloc( p-name+1 );
420 strncpy( tmpname, name, p-name );
421 tmpname[p-name] = '\0';
423 tmpname = (char *)name;
426 if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
427 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
428 if ( tmpname != name )
429 ldap_memfree( tmpname );
430 return( air->air_at );
433 if ( tmpname != name )
434 ldap_memfree( tmpname );
441 AttributeType ***listp
444 AttributeType **list;
445 AttributeType **list1;
451 list = calloc(size, sizeof(AttributeType *));
463 list1 = realloc(list, size*sizeof(AttributeType *));
478 AttributeType ***listp
481 AttributeType **list;
482 AttributeType **list1;
490 for ( i=0; list[i]; i++ )
495 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
499 /* Tell the runtime this can be shrinked */
500 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
519 for ( i=0; list[i]; i++ ) {
520 if ( sat == list[i] ) {
534 struct aindexrec *air;
538 while ( *atp != NULL ) {
539 atp = &(*atp)->sat_next;
543 if ( sat->sat_oid ) {
544 air = (struct aindexrec *)
545 ch_calloc( 1, sizeof(struct aindexrec) );
546 air->air_name = sat->sat_oid;
548 if ( avl_insert( &attr_index, (caddr_t) air,
549 (AVL_CMP) attr_index_cmp,
550 (AVL_DUP) avl_dup_error ) ) {
553 return SLAP_SCHERR_DUP_ATTR;
555 /* FIX: temporal consistency check */
556 at_find(air->air_name);
558 if ( (names = sat->sat_names) ) {
560 air = (struct aindexrec *)
561 ch_calloc( 1, sizeof(struct aindexrec) );
562 air->air_name = ch_strdup(*names);
564 if ( avl_insert( &attr_index, (caddr_t) air,
565 (AVL_CMP) attr_index_cmp,
566 (AVL_DUP) avl_dup_error ) ) {
568 ldap_memfree(air->air_name);
570 return SLAP_SCHERR_DUP_ATTR;
572 /* FIX: temporal consistency check */
573 at_find(air->air_name);
583 LDAP_ATTRIBUTE_TYPE *at,
594 if ( at->at_names && at->at_names[0] ) {
595 errattr = at->at_names[0];
596 } else if ( at->at_oid ) {
597 errattr = at->at_oid;
600 return SLAP_SCHERR_ATTR_INCOMPLETE;
602 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
603 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
605 if ( at->at_sup_oid ) {
606 if ( (sat1 = at_find(at->at_sup_oid)) ) {
608 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
610 return SLAP_SCHERR_OUTOFMEM;
613 *err = at->at_sup_oid;
614 return SLAP_SCHERR_ATTR_NOT_FOUND;
619 * Inherit definitions from superiors. We only check the
620 * direct superior since that one has already inherited from
623 if ( sat->sat_sup ) {
624 sat->sat_syntax = sat->sat_sup->sat_syntax;
626 sat->sat_equality = sat->sat_sup->sat_equality;
627 sat->sat_ordering = sat->sat_sup->sat_ordering;
628 sat->sat_substr = sat->sat_sup->sat_substr;
631 if ( at->at_syntax_oid ) {
632 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
633 sat->sat_syntax = syn;
635 *err = sat->sat_syntax_oid;
636 return SLAP_SCHERR_SYN_NOT_FOUND;
639 #ifdef SLAPD_SCHEMA_COMPAT
640 if ( !strcmp(at->at_syntax_oid, SYNTAX_DS_OID) ) {
641 if ( at->at_equality_oid && (
642 !strcmp(at->at_equality_oid, SYNTAX_DSCE_OID) ) )
644 sat->sat_syntax_compat = SYNTAX_CES;
646 sat->sat_syntax_compat = SYNTAX_CIS;
649 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_IA5_OID) ) {
650 if ( at->at_equality_oid && (
651 !strcmp(at->at_equality_oid, SYNTAX_IA5CE_OID) ) )
653 sat->sat_syntax_compat = SYNTAX_CES;
655 sat->sat_syntax_compat = SYNTAX_CIS;
658 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_DN_OID ) ) {
659 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
661 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_TEL_OID ) ) {
662 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
664 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_BIN_OID ) ) {
665 sat->sat_syntax_compat = SYNTAX_BIN;
668 sat->sat_syntax_compat = DEFAULT_SYNTAX;
672 } else if ( sat->sat_syntax == NULL ) {
673 return SLAP_SCHERR_ATTR_INCOMPLETE;
676 if ( sat->sat_equality_oid ) {
677 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
678 sat->sat_equality = mr;
680 *err = sat->sat_equality_oid;
681 return SLAP_SCHERR_MR_NOT_FOUND;
686 if ( sat->sat_ordering_oid ) {
687 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
688 sat->sat_ordering = mr;
690 *err = sat->sat_ordering_oid;
691 return SLAP_SCHERR_MR_NOT_FOUND;
695 if ( sat->sat_substr_oid ) {
696 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
697 sat->sat_substr = mr;
699 *err = sat->sat_substr_oid;
700 return SLAP_SCHERR_MR_NOT_FOUND;
704 code = at_insert(sat,err);
710 #ifdef SLAPD_SCHEMA_COMPAT
711 at_canonical_name( const char * a_type )
713 at_canonical_name( AttributeType * atp )
716 #ifdef SLAPD_SCHEMA_COMPAT
723 #ifdef SLAPD_SCHEMA_COMPAT
724 return (char *) a_type;
729 } else if ( atp->sat_names
730 && atp->sat_names[0] && (*(atp->sat_names[0]) != '\0') )
732 return atp->sat_names[0];
734 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
738 #ifdef SLAPD_SCHEMA_COMPAT
739 return (char *) a_type;
745 #if defined( SLAPD_SCHEMA_DN )
747 at_schema_info( Entry *e )
750 struct berval *vals[2];
756 for ( at = attr_list; at; at = at->sat_next ) {
757 val.bv_val = ldap_attributetype2str( &at->sat_atype );
759 val.bv_len = strlen( val.bv_val );
760 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
761 (long) val.bv_len, val.bv_val, 0 );
762 attr_merge( e, "attributeTypes", vals );
763 ldap_memfree( val.bv_val );
774 at_index_printnode( struct aindexrec *air )
779 ldap_attributetype2str(&air->air_at->sat_atype) );
784 at_index_print( void )
786 printf("Printing attribute type index:\n");
787 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
788 0, -1, AVL_INORDER );