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>
29 static void at_index_print( void );
33 attr_free( Attribute *a )
36 ber_bvecfree( a->a_vals );
41 attrs_free( Attribute *a )
45 for( ; a != NULL ; a = next ) {
51 Attribute *attr_dup( Attribute *a )
55 if( a == NULL) return NULL;
57 tmp = ch_malloc( sizeof(Attribute) );
59 if( a->a_vals != NULL ) {
62 for( i=0; a->a_vals[i] != NULL; i++ ) {
66 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
68 for( i=0; a->a_vals[i] != NULL; i++ ) {
69 tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
71 if( tmp->a_vals[i] == NULL ) break;
74 tmp->a_vals[i] = NULL;
80 tmp->a_type = ch_strdup( a->a_type );
81 tmp->a_syntax = a->a_syntax;
87 Attribute *attrs_dup( Attribute *a )
89 Attribute *tmp, **next;
91 if( a == NULL ) return NULL;
96 for( ; a != NULL ; a = a->a_next ) {
97 *next = attr_dup( a );
98 next = &((*next)->a_next);
106 * attr_normalize - normalize an attribute name (make it all lowercase)
110 attr_normalize( char *s )
114 return( ldap_pvt_str2lower( s ) );
118 * attr_merge_fast - merge the given type and value with the list of
119 * attributes in attrs. called from str2entry(), where we can make some
120 * assumptions to make things faster.
121 * returns 0 everything went ok
129 struct berval **vals,
137 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
138 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
145 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
146 (**a)->a_type = attr_normalize( ch_strdup( type ) );
147 (**a)->a_vals = NULL;
148 (**a)->a_syntax = attr_syntax( type );
149 (**a)->a_next = NULL;
152 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
157 * attr_merge - merge the given type and value with the list of
158 * attributes in attrs.
159 * returns 0 everything went ok
172 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
173 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
179 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
180 (*a)->a_type = attr_normalize( ch_strdup( type ) );
182 (*a)->a_syntax = attr_syntax( type );
186 return( value_add( &(*a)->a_vals, vals ) );
190 * attr_find - find and return attribute type in list a
199 for ( ; a != NULL; a = a->a_next ) {
200 if ( strcasecmp( a->a_type, type ) == 0 ) {
209 * attr_delete - delete the attribute type in list pointed to by attrs
210 * return 0 deleted ok
211 * 1 not found in list a
212 * -1 something bad happened
224 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
225 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
241 #define DEFAULT_SYNTAX SYNTAX_CIS
244 * attr_syntax - return the syntax of attribute type
248 attr_syntax( char *type )
254 return( sat->sat_syntax_compat );
257 return( DEFAULT_SYNTAX );
261 * attr_syntax_config - process an attribute syntax config line
273 LDAP_ATTRIBUTE_TYPE *at;
279 Debug( LDAP_DEBUG_ANY,
280 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
285 at = (LDAP_ATTRIBUTE_TYPE *)
286 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
289 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
290 strcasecmp( argv[lasti], "cis" ) == 0 ) {
291 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
292 at->at_equality_oid = "2.5.13.2";
293 at->at_ordering_oid = "2.5.13.3";
294 at->at_substr_oid = "2.5.13.4";
295 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
296 strcasecmp( argv[lasti], "tel" ) == 0 ) {
297 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
298 at->at_equality_oid = "2.5.13.20";
299 at->at_substr_oid = "2.5.13.21";
300 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
301 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
302 at->at_equality_oid = "2.5.13.1";
303 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
304 strcasecmp( argv[lasti], "ces" ) == 0 ) {
305 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
306 at->at_equality_oid = "2.5.13.5";
307 at->at_ordering_oid = "2.5.13.6";
308 at->at_substr_oid = "2.5.13.7";
309 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
310 strcasecmp( argv[lasti], "bin" ) == 0 ) {
311 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
312 /* There is no match for binary syntax. Really */
314 Debug( LDAP_DEBUG_ANY,
315 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
316 fname, lineno, argv[lasti] );
317 Debug( LDAP_DEBUG_ANY,
318 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
320 free( (AttributeType *) at );
326 at->at_names = charray_dup( argv );
329 code = at_add( at, &err );
331 fprintf( stderr, "%s: line %d: %s %s\n",
332 fname, lineno, scherr2str(code), err);
333 exit( EXIT_FAILURE );
345 if ( at_find( name ) ) {
351 attr_syntax_config( "implicit", 0, 2, argv );
358 AttributeType *air_at;
361 static Avlnode *attr_index = NULL;
362 static AttributeType *attr_list = NULL;
366 struct aindexrec *air1,
367 struct aindexrec *air2
370 return (strcasecmp( air1->air_name, air2->air_name ));
376 struct aindexrec *air
379 return (strcasecmp( type, air->air_name ));
387 struct aindexrec *air = NULL;
389 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
390 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
391 return( air->air_at );
399 AttributeType ***listp
402 AttributeType **list;
403 AttributeType **list1;
409 list = calloc(size, sizeof(AttributeType *));
421 list1 = realloc(list, size*sizeof(AttributeType *));
436 AttributeType ***listp
439 AttributeType **list;
440 AttributeType **list1;
448 for ( i=0; list[i]; i++ )
453 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
457 /* Tell the runtime this can be shrinked */
458 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
477 for ( i=0; list[i]; i++ ) {
478 if ( sat == list[i] ) {
492 struct aindexrec *air;
496 while ( *atp != NULL ) {
497 atp = &(*atp)->sat_next;
501 if ( sat->sat_oid ) {
502 air = (struct aindexrec *)
503 ch_calloc( 1, sizeof(struct aindexrec) );
504 air->air_name = sat->sat_oid;
506 if ( avl_insert( &attr_index, (caddr_t) air,
507 (AVL_CMP) attr_index_cmp,
508 (AVL_DUP) avl_dup_error ) ) {
511 return SLAP_SCHERR_DUP_ATTR;
513 /* FIX: temporal consistency check */
514 at_find(air->air_name);
516 if ( (names = sat->sat_names) ) {
518 air = (struct aindexrec *)
519 ch_calloc( 1, sizeof(struct aindexrec) );
520 air->air_name = ch_strdup(*names);
522 if ( avl_insert( &attr_index, (caddr_t) air,
523 (AVL_CMP) attr_index_cmp,
524 (AVL_DUP) avl_dup_error ) ) {
527 return SLAP_SCHERR_DUP_ATTR;
529 /* FIX: temporal consistency check */
530 at_find(air->air_name);
540 LDAP_ATTRIBUTE_TYPE *at,
551 if ( at->at_names && at->at_names[0] ) {
552 errattr = at->at_names[0];
553 } else if ( at->at_oid ) {
554 errattr = at->at_oid;
557 return SLAP_SCHERR_ATTR_INCOMPLETE;
559 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
560 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
561 if ( at->at_sup_oid ) {
562 if ( (sat1 = at_find(at->at_sup_oid)) ) {
564 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
566 return SLAP_SCHERR_OUTOFMEM;
569 *err = at->at_sup_oid;
570 return SLAP_SCHERR_ATTR_NOT_FOUND;
574 if ( at->at_syntax_oid ) {
575 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
576 sat->sat_syntax = syn;
578 *err = sat->sat_syntax_oid;
579 return SLAP_SCHERR_SYN_NOT_FOUND;
581 if ( !strcmp(at->at_syntax_oid,
582 "1.3.6.1.4.1.1466.115.121.1.15") ) {
583 if ( at->at_equality_oid &&
584 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
585 sat->sat_syntax_compat = SYNTAX_CES;
587 sat->sat_syntax_compat = SYNTAX_CIS;
589 } else if ( !strcmp(at->at_syntax_oid,
590 "1.3.6.1.4.1.1466.115.121.1.50") ) {
591 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
592 } else if ( !strcmp(at->at_syntax_oid,
593 "1.3.6.1.4.1.1466.115.121.1.12") ) {
594 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
595 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
596 sat->sat_syntax_compat = SYNTAX_BIN;
598 sat->sat_syntax_compat = DEFAULT_SYNTAX;
601 sat->sat_syntax_compat = DEFAULT_SYNTAX;
604 if ( sat->sat_equality_oid ) {
605 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
606 sat->sat_equality = mr;
608 *err = sat->sat_equality_oid;
609 return SLAP_SCHERR_MR_NOT_FOUND;
612 if ( sat->sat_ordering_oid ) {
613 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
614 sat->sat_ordering = mr;
616 *err = sat->sat_ordering_oid;
617 return SLAP_SCHERR_MR_NOT_FOUND;
620 if ( sat->sat_substr_oid ) {
621 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
622 sat->sat_substr = mr;
624 *err = sat->sat_substr_oid;
625 return SLAP_SCHERR_MR_NOT_FOUND;
630 * Now inherit definitions from superiors. We only check the
631 * direct superior since that one has already inherited from
634 if ( sat->sat_sup ) {
635 if ( !sat->sat_syntax ) {
636 sat->sat_syntax = sat->sat_sup->sat_syntax;
637 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
639 if ( !sat->sat_equality ) {
640 sat->sat_equality = sat->sat_sup->sat_equality;
642 if ( !sat->sat_ordering ) {
643 sat->sat_ordering = sat->sat_sup->sat_ordering;
645 if ( !sat->sat_substr ) {
646 sat->sat_substr = sat->sat_sup->sat_substr;
649 code = at_insert(sat,err);
655 at_canonical_name( char * a_type )
659 if ( (atp=at_find(a_type)) == NULL ) {
663 } else if ( atp->sat_names
665 && (*(atp->sat_names[0]) != '\0') ) {
667 return atp->sat_names[0];
669 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
679 }/* char * at_canonica_name() */
682 #if defined( SLAPD_SCHEMA_DN )
685 at_schema_info( Entry *e )
688 struct berval *vals[2];
694 for ( at = attr_list; at; at = at->sat_next ) {
695 val.bv_val = ldap_attributetype2str( &at->sat_atype );
697 val.bv_len = strlen( val.bv_val );
698 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
699 (long) val.bv_len, val.bv_val, 0 );
700 attr_merge( e, "attributeTypes", vals );
701 ldap_memfree( val.bv_val );
713 at_index_printnode( struct aindexrec *air )
716 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
721 at_index_print( void )
723 printf("Printing attribute type index:\n");
724 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
725 0, -1, AVL_INORDER );