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 * attr_normalize - normalize an attribute name (make it all lowercase)
41 attr_normalize( char *s )
47 for ( save = s; *s; s++ ) {
48 *s = TOLOWER( (unsigned char) *s );
55 * attr_merge_fast - merge the given type and value with the list of
56 * attributes in attrs. called from str2entry(), where we can make some
57 * assumptions to make things faster.
58 * returns 0 everything went ok
74 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
75 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
82 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
83 (**a)->a_type = attr_normalize( ch_strdup( type ) );
85 (**a)->a_syntax = attr_syntax( type );
89 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
94 * attr_merge - merge the given type and value with the list of
95 * attributes in attrs.
96 * returns 0 everything went ok
109 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
110 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
116 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
117 (*a)->a_type = attr_normalize( ch_strdup( type ) );
119 (*a)->a_syntax = attr_syntax( type );
123 return( value_add( &(*a)->a_vals, vals ) );
127 * attr_find - find and return attribute type in list a
136 for ( ; a != NULL; a = a->a_next ) {
137 if ( strcasecmp( a->a_type, type ) == 0 ) {
146 * attr_delete - delete the attribute type in list pointed to by attrs
147 * return 0 deleted ok
148 * 1 not found in list a
149 * -1 something bad happened
161 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
162 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
178 #define DEFAULT_SYNTAX SYNTAX_CIS
181 * attr_syntax - return the syntax of attribute type
185 attr_syntax( char *type )
191 return( sat->sat_syntax_compat );
194 return( DEFAULT_SYNTAX );
198 * attr_syntax_config - process an attribute syntax config line
210 LDAP_ATTRIBUTE_TYPE *at;
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
222 at = (LDAP_ATTRIBUTE_TYPE *)
223 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
226 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
227 strcasecmp( argv[lasti], "cis" ) == 0 ) {
228 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
229 at->at_equality_oid = "2.5.13.2";
230 at->at_ordering_oid = "2.5.13.3";
231 at->at_substr_oid = "2.5.13.4";
232 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
233 strcasecmp( argv[lasti], "tel" ) == 0 ) {
234 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
235 at->at_equality_oid = "2.5.13.20";
236 at->at_substr_oid = "2.5.13.21";
237 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
238 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
239 at->at_equality_oid = "2.5.13.1";
240 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
241 strcasecmp( argv[lasti], "ces" ) == 0 ) {
242 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
243 at->at_equality_oid = "2.5.13.5";
244 at->at_ordering_oid = "2.5.13.6";
245 at->at_substr_oid = "2.5.13.7";
246 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
247 strcasecmp( argv[lasti], "bin" ) == 0 ) {
248 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
249 /* There is no match for binary syntax. Really */
251 Debug( LDAP_DEBUG_ANY,
252 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
253 fname, lineno, argv[lasti] );
254 Debug( LDAP_DEBUG_ANY,
255 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
257 free( (AttributeType *) at );
263 at->at_names = charray_dup( argv );
266 code = at_add( at, &err );
268 fprintf( stderr, "%s: line %d: %s %s\n",
269 fname, lineno, scherr2str(code), err);
282 if ( at_find( name ) ) {
288 attr_syntax_config( "implicit", 0, 2, argv );
295 AttributeType *air_at;
298 static Avlnode *attr_index = NULL;
299 static AttributeType *attr_list = NULL;
303 struct aindexrec *air1,
304 struct aindexrec *air2
307 return (strcasecmp( air1->air_name, air2->air_name ));
313 struct aindexrec *air
316 return (strcasecmp( type, air->air_name ));
324 struct aindexrec *air = NULL;
326 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
327 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
328 return( air->air_at );
336 AttributeType ***listp
339 AttributeType **list;
340 AttributeType **list1;
346 list = calloc(size, sizeof(AttributeType *));
358 list1 = realloc(list, size*sizeof(AttributeType *));
373 AttributeType ***listp
376 AttributeType **list;
377 AttributeType **list1;
385 for ( i=0; list[i]; i++ )
390 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
394 /* Tell the runtime this can be shrinked */
395 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
414 for ( i=0; list[i]; i++ ) {
415 if ( sat == list[i] ) {
429 struct aindexrec *air;
433 while ( *atp != NULL ) {
434 atp = &(*atp)->sat_next;
438 if ( sat->sat_oid ) {
439 air = (struct aindexrec *)
440 ch_calloc( 1, sizeof(struct aindexrec) );
441 air->air_name = sat->sat_oid;
443 if ( avl_insert( &attr_index, (caddr_t) air,
444 (AVL_CMP) attr_index_cmp,
445 (AVL_DUP) avl_dup_error ) ) {
448 return SLAP_SCHERR_DUP_ATTR;
450 /* FIX: temporal consistency check */
451 at_find(air->air_name);
453 if ( (names = sat->sat_names) ) {
455 air = (struct aindexrec *)
456 ch_calloc( 1, sizeof(struct aindexrec) );
457 air->air_name = ch_strdup(*names);
459 if ( avl_insert( &attr_index, (caddr_t) air,
460 (AVL_CMP) attr_index_cmp,
461 (AVL_DUP) avl_dup_error ) ) {
464 return SLAP_SCHERR_DUP_ATTR;
466 /* FIX: temporal consistency check */
467 at_find(air->air_name);
477 LDAP_ATTRIBUTE_TYPE *at,
488 if ( at->at_names && at->at_names[0] ) {
489 errattr = at->at_names[0];
490 } else if ( at->at_oid ) {
491 errattr = at->at_oid;
494 return SLAP_SCHERR_ATTR_INCOMPLETE;
496 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
497 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
498 if ( at->at_sup_oid ) {
499 if ( (sat1 = at_find(at->at_sup_oid)) ) {
501 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
503 return SLAP_SCHERR_OUTOFMEM;
506 *err = at->at_sup_oid;
507 return SLAP_SCHERR_ATTR_NOT_FOUND;
511 if ( at->at_syntax_oid ) {
512 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
513 sat->sat_syntax = syn;
515 *err = sat->sat_syntax_oid;
516 return SLAP_SCHERR_SYN_NOT_FOUND;
518 if ( !strcmp(at->at_syntax_oid,
519 "1.3.6.1.4.1.1466.115.121.1.15") ) {
520 if ( at->at_equality_oid &&
521 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
522 sat->sat_syntax_compat = SYNTAX_CES;
524 sat->sat_syntax_compat = SYNTAX_CIS;
526 } else if ( !strcmp(at->at_syntax_oid,
527 "1.3.6.1.4.1.1466.115.121.1.50") ) {
528 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
529 } else if ( !strcmp(at->at_syntax_oid,
530 "1.3.6.1.4.1.1466.115.121.1.12") ) {
531 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
532 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
533 sat->sat_syntax_compat = SYNTAX_BIN;
535 sat->sat_syntax_compat = DEFAULT_SYNTAX;
538 sat->sat_syntax_compat = DEFAULT_SYNTAX;
541 if ( sat->sat_equality_oid ) {
542 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
543 sat->sat_equality = mr;
545 *err = sat->sat_equality_oid;
546 return SLAP_SCHERR_MR_NOT_FOUND;
549 if ( sat->sat_ordering_oid ) {
550 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
551 sat->sat_ordering = mr;
553 *err = sat->sat_ordering_oid;
554 return SLAP_SCHERR_MR_NOT_FOUND;
557 if ( sat->sat_substr_oid ) {
558 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
559 sat->sat_substr = mr;
561 *err = sat->sat_substr_oid;
562 return SLAP_SCHERR_MR_NOT_FOUND;
567 * Now inherit definitions from superiors. We only check the
568 * direct superior since that one has already inherited from
571 if ( sat->sat_sup ) {
572 if ( !sat->sat_syntax ) {
573 sat->sat_syntax = sat->sat_sup->sat_syntax;
574 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
576 if ( !sat->sat_equality ) {
577 sat->sat_equality = sat->sat_sup->sat_equality;
579 if ( !sat->sat_ordering ) {
580 sat->sat_ordering = sat->sat_sup->sat_ordering;
582 if ( !sat->sat_substr ) {
583 sat->sat_substr = sat->sat_sup->sat_substr;
586 code = at_insert(sat,err);
592 at_canonical_name( char * a_type )
596 if ( (atp=at_find(a_type)) == NULL ) {
600 } else if ( atp->sat_names
602 && (*(atp->sat_names[0]) != '\0') ) {
604 return atp->sat_names[0];
606 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
616 }/* char * at_canonica_name() */
619 #if defined( SLAPD_SCHEMA_DN )
622 at_schema_info( Entry *e )
625 struct berval *vals[2];
631 for ( at = attr_list; at; at = at->sat_next ) {
632 val.bv_val = ldap_attributetype2str( &at->sat_atype );
634 val.bv_len = strlen( val.bv_val );
635 Debug( LDAP_DEBUG_TRACE, "Merging at [%d] %s\n",
636 val.bv_len, val.bv_val, 0 );
637 attr_merge( e, "attributeTypes", vals );
638 ldap_memfree( val.bv_val );
650 at_index_printnode( struct aindexrec *air )
653 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
658 at_index_print( void )
660 printf("Printing attribute type index:\n");
661 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
662 0, -1, AVL_INORDER );