1 /* attr.c - routines for dealing with attributes */
13 #include <ac/socket.h>
14 #include <ac/string.h>
17 #ifdef HAVE_SYS_PARAM_H
18 #include <sys/param.h>
21 #include "ldap_defaults.h"
25 static void at_index_print( void );
29 attr_free( Attribute *a )
32 ber_bvecfree( a->a_vals );
37 attrs_free( Attribute *a )
41 for( ; a != NULL ; a = next ) {
47 Attribute *attr_dup( Attribute *a )
51 if( a == NULL) return NULL;
53 tmp = ch_malloc( sizeof(Attribute) );
55 if( a->a_vals != NULL ) {
58 for( i=0; a->a_vals[i] != NULL; i++ ) {
62 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
64 for( i=0; a->a_vals[i] != NULL; i++ ) {
65 tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
68 tmp->a_vals[i] = NULL;
74 tmp->a_type = ch_strdup( a->a_type );
75 tmp->a_syntax = a->a_syntax;
81 Attribute *attrs_dup( Attribute *a )
83 Attribute *tmp, **next;
85 if( a == NULL ) return NULL;
90 for( ; a != NULL ; a = a->a_next ) {
91 *next = attr_dup( a );
92 next = &((*next)->a_next);
100 * attr_normalize - normalize an attribute name (make it all lowercase)
104 attr_normalize( char *s )
108 return( str2lower( s ) );
112 * attr_merge_fast - merge the given type and value with the list of
113 * attributes in attrs. called from str2entry(), where we can make some
114 * assumptions to make things faster.
115 * returns 0 everything went ok
123 struct berval **vals,
131 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
132 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
139 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
140 (**a)->a_type = attr_normalize( ch_strdup( type ) );
141 (**a)->a_vals = NULL;
142 (**a)->a_syntax = attr_syntax( type );
143 (**a)->a_next = NULL;
146 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
151 * attr_merge - merge the given type and value with the list of
152 * attributes in attrs.
153 * returns 0 everything went ok
166 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
167 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
173 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
174 (*a)->a_type = attr_normalize( ch_strdup( type ) );
176 (*a)->a_syntax = attr_syntax( type );
180 return( value_add( &(*a)->a_vals, vals ) );
184 * attr_find - find and return attribute type in list a
193 for ( ; a != NULL; a = a->a_next ) {
194 if ( strcasecmp( a->a_type, type ) == 0 ) {
203 * attr_delete - delete the attribute type in list pointed to by attrs
204 * return 0 deleted ok
205 * 1 not found in list a
206 * -1 something bad happened
218 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
219 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
235 #define DEFAULT_SYNTAX SYNTAX_CIS
238 * attr_syntax - return the syntax of attribute type
242 attr_syntax( char *type )
248 return( sat->sat_syntax_compat );
251 return( DEFAULT_SYNTAX );
255 * attr_syntax_config - process an attribute syntax config line
267 LDAP_ATTRIBUTE_TYPE *at;
273 Debug( LDAP_DEBUG_ANY,
274 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
279 at = (LDAP_ATTRIBUTE_TYPE *)
280 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
283 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
284 strcasecmp( argv[lasti], "cis" ) == 0 ) {
285 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
286 at->at_equality_oid = "2.5.13.2";
287 at->at_ordering_oid = "2.5.13.3";
288 at->at_substr_oid = "2.5.13.4";
289 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
290 strcasecmp( argv[lasti], "tel" ) == 0 ) {
291 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
292 at->at_equality_oid = "2.5.13.20";
293 at->at_substr_oid = "2.5.13.21";
294 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
295 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
296 at->at_equality_oid = "2.5.13.1";
297 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
298 strcasecmp( argv[lasti], "ces" ) == 0 ) {
299 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
300 at->at_equality_oid = "2.5.13.5";
301 at->at_ordering_oid = "2.5.13.6";
302 at->at_substr_oid = "2.5.13.7";
303 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
304 strcasecmp( argv[lasti], "bin" ) == 0 ) {
305 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
306 /* There is no match for binary syntax. Really */
308 Debug( LDAP_DEBUG_ANY,
309 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
310 fname, lineno, argv[lasti] );
311 Debug( LDAP_DEBUG_ANY,
312 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
314 free( (AttributeType *) at );
320 at->at_names = charray_dup( argv );
323 code = at_add( at, &err );
325 fprintf( stderr, "%s: line %d: %s %s\n",
326 fname, lineno, scherr2str(code), err);
339 if ( at_find( name ) ) {
345 attr_syntax_config( "implicit", 0, 2, argv );
352 AttributeType *air_at;
355 static Avlnode *attr_index = NULL;
356 static AttributeType *attr_list = NULL;
360 struct aindexrec *air1,
361 struct aindexrec *air2
364 return (strcasecmp( air1->air_name, air2->air_name ));
370 struct aindexrec *air
373 return (strcasecmp( type, air->air_name ));
381 struct aindexrec *air = NULL;
383 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
384 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
385 return( air->air_at );
393 AttributeType ***listp
396 AttributeType **list;
397 AttributeType **list1;
403 list = calloc(size, sizeof(AttributeType *));
415 list1 = realloc(list, size*sizeof(AttributeType *));
430 AttributeType ***listp
433 AttributeType **list;
434 AttributeType **list1;
442 for ( i=0; list[i]; i++ )
447 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
451 /* Tell the runtime this can be shrinked */
452 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
471 for ( i=0; list[i]; i++ ) {
472 if ( sat == list[i] ) {
486 struct aindexrec *air;
490 while ( *atp != NULL ) {
491 atp = &(*atp)->sat_next;
495 if ( sat->sat_oid ) {
496 air = (struct aindexrec *)
497 ch_calloc( 1, sizeof(struct aindexrec) );
498 air->air_name = sat->sat_oid;
500 if ( avl_insert( &attr_index, (caddr_t) air,
501 (AVL_CMP) attr_index_cmp,
502 (AVL_DUP) avl_dup_error ) ) {
505 return SLAP_SCHERR_DUP_ATTR;
507 /* FIX: temporal consistency check */
508 at_find(air->air_name);
510 if ( (names = sat->sat_names) ) {
512 air = (struct aindexrec *)
513 ch_calloc( 1, sizeof(struct aindexrec) );
514 air->air_name = ch_strdup(*names);
516 if ( avl_insert( &attr_index, (caddr_t) air,
517 (AVL_CMP) attr_index_cmp,
518 (AVL_DUP) avl_dup_error ) ) {
521 return SLAP_SCHERR_DUP_ATTR;
523 /* FIX: temporal consistency check */
524 at_find(air->air_name);
534 LDAP_ATTRIBUTE_TYPE *at,
545 if ( at->at_names && at->at_names[0] ) {
546 errattr = at->at_names[0];
547 } else if ( at->at_oid ) {
548 errattr = at->at_oid;
551 return SLAP_SCHERR_ATTR_INCOMPLETE;
553 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
554 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
555 if ( at->at_sup_oid ) {
556 if ( (sat1 = at_find(at->at_sup_oid)) ) {
558 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
560 return SLAP_SCHERR_OUTOFMEM;
563 *err = at->at_sup_oid;
564 return SLAP_SCHERR_ATTR_NOT_FOUND;
568 if ( at->at_syntax_oid ) {
569 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
570 sat->sat_syntax = syn;
572 *err = sat->sat_syntax_oid;
573 return SLAP_SCHERR_SYN_NOT_FOUND;
575 if ( !strcmp(at->at_syntax_oid,
576 "1.3.6.1.4.1.1466.115.121.1.15") ) {
577 if ( at->at_equality_oid &&
578 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
579 sat->sat_syntax_compat = SYNTAX_CES;
581 sat->sat_syntax_compat = SYNTAX_CIS;
583 } else if ( !strcmp(at->at_syntax_oid,
584 "1.3.6.1.4.1.1466.115.121.1.50") ) {
585 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
586 } else if ( !strcmp(at->at_syntax_oid,
587 "1.3.6.1.4.1.1466.115.121.1.12") ) {
588 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
589 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
590 sat->sat_syntax_compat = SYNTAX_BIN;
592 sat->sat_syntax_compat = DEFAULT_SYNTAX;
595 sat->sat_syntax_compat = DEFAULT_SYNTAX;
598 if ( sat->sat_equality_oid ) {
599 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
600 sat->sat_equality = mr;
602 *err = sat->sat_equality_oid;
603 return SLAP_SCHERR_MR_NOT_FOUND;
606 if ( sat->sat_ordering_oid ) {
607 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
608 sat->sat_ordering = mr;
610 *err = sat->sat_ordering_oid;
611 return SLAP_SCHERR_MR_NOT_FOUND;
614 if ( sat->sat_substr_oid ) {
615 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
616 sat->sat_substr = mr;
618 *err = sat->sat_substr_oid;
619 return SLAP_SCHERR_MR_NOT_FOUND;
624 * Now inherit definitions from superiors. We only check the
625 * direct superior since that one has already inherited from
628 if ( sat->sat_sup ) {
629 if ( !sat->sat_syntax ) {
630 sat->sat_syntax = sat->sat_sup->sat_syntax;
631 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
633 if ( !sat->sat_equality ) {
634 sat->sat_equality = sat->sat_sup->sat_equality;
636 if ( !sat->sat_ordering ) {
637 sat->sat_ordering = sat->sat_sup->sat_ordering;
639 if ( !sat->sat_substr ) {
640 sat->sat_substr = sat->sat_sup->sat_substr;
643 code = at_insert(sat,err);
649 at_canonical_name( char * a_type )
653 if ( (atp=at_find(a_type)) == NULL ) {
657 } else if ( atp->sat_names
659 && (*(atp->sat_names[0]) != '\0') ) {
661 return atp->sat_names[0];
663 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
673 }/* char * at_canonica_name() */
676 #if defined( SLAPD_SCHEMA_DN )
679 at_schema_info( Entry *e )
682 struct berval *vals[2];
688 for ( at = attr_list; at; at = at->sat_next ) {
689 val.bv_val = ldap_attributetype2str( &at->sat_atype );
691 val.bv_len = strlen( val.bv_val );
692 Debug( LDAP_DEBUG_TRACE, "Merging at [%d] %s\n",
693 val.bv_len, val.bv_val, 0 );
694 attr_merge( e, "attributeTypes", vals );
695 ldap_memfree( val.bv_val );
707 at_index_printnode( struct aindexrec *air )
710 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
715 at_index_print( void )
717 printf("Printing attribute type index:\n");
718 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
719 0, -1, AVL_INORDER );