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 "ldapconfig.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 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
231 strcasecmp( argv[lasti], "tel" ) == 0 ) {
232 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
233 at->at_equality_oid = "2.5.13.20";
234 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
235 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
236 at->at_equality_oid = "2.5.13.1";
237 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
238 strcasecmp( argv[lasti], "ces" ) == 0 ) {
239 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
240 /* notice: this is caseExactIA5Match */
241 at->at_equality_oid = "1.3.6.1.4.1.1466.109.114.1";
242 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
243 strcasecmp( argv[lasti], "bin" ) == 0 ) {
244 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
245 /* There is no match for binary syntax. Really */
247 Debug( LDAP_DEBUG_ANY,
248 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
249 fname, lineno, argv[lasti] );
250 Debug( LDAP_DEBUG_ANY,
251 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
253 free( (AttributeType *) at );
259 at->at_names = charray_dup( argv );
262 code = at_add( at, &err );
264 fprintf( stderr, "%s: line %d: %s %s\n",
265 fname, lineno, scherr2str(code), err);
278 if ( at_find( name ) ) {
284 attr_syntax_config( "implicit", 0, 2, argv );
291 AttributeType *air_at;
294 static Avlnode *attr_index = NULL;
295 static AttributeType *attr_list = NULL;
299 struct aindexrec *air1,
300 struct aindexrec *air2
303 return (strcasecmp( air1->air_name, air2->air_name ));
309 struct aindexrec *air
312 return (strcasecmp( type, air->air_name ));
320 struct aindexrec *air = NULL;
322 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
323 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
324 return( air->air_at );
332 AttributeType ***listp
335 AttributeType **list;
336 AttributeType **list1;
342 list = calloc(size, sizeof(AttributeType *));
354 list1 = realloc(list, size*sizeof(AttributeType *));
369 AttributeType ***listp
372 AttributeType **list;
373 AttributeType **list1;
381 for ( i=0; list[i]; i++ )
386 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
390 /* Tell the runtime this can be shrinked */
391 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
410 for ( i=0; list[i]; i++ ) {
411 if ( sat == list[i] ) {
425 struct aindexrec *air;
429 while ( *atp != NULL ) {
430 atp = &(*atp)->sat_next;
434 if ( sat->sat_oid ) {
435 air = (struct aindexrec *)
436 ch_calloc( 1, sizeof(struct aindexrec) );
437 air->air_name = sat->sat_oid;
439 if ( avl_insert( &attr_index, (caddr_t) air,
440 (AVL_CMP) attr_index_cmp,
441 (AVL_DUP) avl_dup_error ) ) {
444 return SLAP_SCHERR_DUP_ATTR;
446 /* FIX: temporal consistency check */
447 at_find(air->air_name);
449 if ( (names = sat->sat_names) ) {
451 air = (struct aindexrec *)
452 ch_calloc( 1, sizeof(struct aindexrec) );
453 air->air_name = ch_strdup(*names);
455 if ( avl_insert( &attr_index, (caddr_t) air,
456 (AVL_CMP) attr_index_cmp,
457 (AVL_DUP) avl_dup_error ) ) {
460 return SLAP_SCHERR_DUP_ATTR;
462 /* FIX: temporal consistency check */
463 at_find(air->air_name);
473 LDAP_ATTRIBUTE_TYPE *at,
484 if ( at->at_names && at->at_names[0] ) {
485 errattr = at->at_names[0];
486 } else if ( at->at_oid ) {
487 errattr = at->at_oid;
490 return SLAP_SCHERR_ATTR_INCOMPLETE;
492 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
493 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
494 if ( at->at_sup_oid ) {
495 if ( (sat1 = at_find(at->at_sup_oid)) ) {
497 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
499 return SLAP_SCHERR_OUTOFMEM;
502 *err = at->at_sup_oid;
503 return SLAP_SCHERR_ATTR_NOT_FOUND;
507 if ( at->at_syntax_oid ) {
509 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
510 sat->sat_syntax = syn;
512 *err = sat->sat_syntax_oid;
513 return SLAP_SCHERR_SYN_NOT_FOUND;
516 if ( !strcmp(at->at_syntax_oid,
517 "1.3.6.1.4.1.1466.115.121.1.15") ) {
518 if ( at->at_equality_oid &&
519 !strcmp(at->at_equality_oid,
520 "1.3.6.1.4.1.1466.109.114.1") ) {
521 sat->sat_syntax_compat = SYNTAX_CES;
523 sat->sat_syntax_compat = SYNTAX_CIS;
525 } else if ( !strcmp(at->at_syntax_oid,
526 "1.3.6.1.4.1.1466.115.121.1.50") ) {
527 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
528 } else if ( !strcmp(at->at_syntax_oid,
529 "1.3.6.1.4.1.1466.115.121.1.12") ) {
530 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
531 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
532 sat->sat_syntax_compat = SYNTAX_BIN;
534 sat->sat_syntax_compat = DEFAULT_SYNTAX;
537 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;
566 code = at_insert(sat,err);
572 at_canonical_name( char * a_type )
576 if ( (atp=at_find(a_type)) == NULL ) {
580 } else if ( atp->sat_names
582 && (*(atp->sat_names[0]) != '\0') ) {
584 return atp->sat_names[0];
586 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
596 }/* char * at_canonica_name() */
599 #if defined( SLAPD_SCHEMA_DN )
602 at_schema_info( Entry *e )
605 struct berval *vals[2];
611 for ( at = attr_list; at; at = at->sat_next ) {
612 val.bv_val = ldap_attributetype2str( &at->sat_atype );
614 val.bv_len = strlen( val.bv_val );
615 Debug( LDAP_DEBUG_TRACE, "Merging at [%d] %s\n",
616 val.bv_len, val.bv_val, 0 );
617 attr_merge( e, "attributeTypes", vals );
618 ldap_memfree( val.bv_val );
630 at_index_printnode( struct aindexrec *air )
633 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
638 at_index_print( void )
640 printf("Printing attribute type index:\n");
641 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
642 0, -1, AVL_INORDER );