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 )
45 return( str2lower( s ) );
49 * attr_merge_fast - merge the given type and value with the list of
50 * attributes in attrs. called from str2entry(), where we can make some
51 * assumptions to make things faster.
52 * returns 0 everything went ok
68 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
69 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
76 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
77 (**a)->a_type = attr_normalize( ch_strdup( type ) );
79 (**a)->a_syntax = attr_syntax( type );
83 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
88 * attr_merge - merge the given type and value with the list of
89 * attributes in attrs.
90 * returns 0 everything went ok
103 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
104 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
110 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
111 (*a)->a_type = attr_normalize( ch_strdup( type ) );
113 (*a)->a_syntax = attr_syntax( type );
117 return( value_add( &(*a)->a_vals, vals ) );
121 * attr_find - find and return attribute type in list a
130 for ( ; a != NULL; a = a->a_next ) {
131 if ( strcasecmp( a->a_type, type ) == 0 ) {
140 * attr_delete - delete the attribute type in list pointed to by attrs
141 * return 0 deleted ok
142 * 1 not found in list a
143 * -1 something bad happened
155 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
156 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
172 #define DEFAULT_SYNTAX SYNTAX_CIS
175 * attr_syntax - return the syntax of attribute type
179 attr_syntax( char *type )
185 return( sat->sat_syntax_compat );
188 return( DEFAULT_SYNTAX );
192 * attr_syntax_config - process an attribute syntax config line
204 LDAP_ATTRIBUTE_TYPE *at;
210 Debug( LDAP_DEBUG_ANY,
211 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
216 at = (LDAP_ATTRIBUTE_TYPE *)
217 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
220 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
221 strcasecmp( argv[lasti], "cis" ) == 0 ) {
222 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
223 at->at_equality_oid = "2.5.13.2";
224 at->at_ordering_oid = "2.5.13.3";
225 at->at_substr_oid = "2.5.13.4";
226 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
227 strcasecmp( argv[lasti], "tel" ) == 0 ) {
228 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
229 at->at_equality_oid = "2.5.13.20";
230 at->at_substr_oid = "2.5.13.21";
231 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
232 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
233 at->at_equality_oid = "2.5.13.1";
234 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
235 strcasecmp( argv[lasti], "ces" ) == 0 ) {
236 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
237 at->at_equality_oid = "2.5.13.5";
238 at->at_ordering_oid = "2.5.13.6";
239 at->at_substr_oid = "2.5.13.7";
240 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
241 strcasecmp( argv[lasti], "bin" ) == 0 ) {
242 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
243 /* There is no match for binary syntax. Really */
245 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
247 fname, lineno, argv[lasti] );
248 Debug( LDAP_DEBUG_ANY,
249 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
251 free( (AttributeType *) at );
257 at->at_names = charray_dup( argv );
260 code = at_add( at, &err );
262 fprintf( stderr, "%s: line %d: %s %s\n",
263 fname, lineno, scherr2str(code), err);
276 if ( at_find( name ) ) {
282 attr_syntax_config( "implicit", 0, 2, argv );
289 AttributeType *air_at;
292 static Avlnode *attr_index = NULL;
293 static AttributeType *attr_list = NULL;
297 struct aindexrec *air1,
298 struct aindexrec *air2
301 return (strcasecmp( air1->air_name, air2->air_name ));
307 struct aindexrec *air
310 return (strcasecmp( type, air->air_name ));
318 struct aindexrec *air = NULL;
320 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
321 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
322 return( air->air_at );
330 AttributeType ***listp
333 AttributeType **list;
334 AttributeType **list1;
340 list = calloc(size, sizeof(AttributeType *));
352 list1 = realloc(list, size*sizeof(AttributeType *));
367 AttributeType ***listp
370 AttributeType **list;
371 AttributeType **list1;
379 for ( i=0; list[i]; i++ )
384 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
388 /* Tell the runtime this can be shrinked */
389 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
408 for ( i=0; list[i]; i++ ) {
409 if ( sat == list[i] ) {
423 struct aindexrec *air;
427 while ( *atp != NULL ) {
428 atp = &(*atp)->sat_next;
432 if ( sat->sat_oid ) {
433 air = (struct aindexrec *)
434 ch_calloc( 1, sizeof(struct aindexrec) );
435 air->air_name = sat->sat_oid;
437 if ( avl_insert( &attr_index, (caddr_t) air,
438 (AVL_CMP) attr_index_cmp,
439 (AVL_DUP) avl_dup_error ) ) {
442 return SLAP_SCHERR_DUP_ATTR;
444 /* FIX: temporal consistency check */
445 at_find(air->air_name);
447 if ( (names = sat->sat_names) ) {
449 air = (struct aindexrec *)
450 ch_calloc( 1, sizeof(struct aindexrec) );
451 air->air_name = ch_strdup(*names);
453 if ( avl_insert( &attr_index, (caddr_t) air,
454 (AVL_CMP) attr_index_cmp,
455 (AVL_DUP) avl_dup_error ) ) {
458 return SLAP_SCHERR_DUP_ATTR;
460 /* FIX: temporal consistency check */
461 at_find(air->air_name);
471 LDAP_ATTRIBUTE_TYPE *at,
482 if ( at->at_names && at->at_names[0] ) {
483 errattr = at->at_names[0];
484 } else if ( at->at_oid ) {
485 errattr = at->at_oid;
488 return SLAP_SCHERR_ATTR_INCOMPLETE;
490 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
491 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
492 if ( at->at_sup_oid ) {
493 if ( (sat1 = at_find(at->at_sup_oid)) ) {
495 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
497 return SLAP_SCHERR_OUTOFMEM;
500 *err = at->at_sup_oid;
501 return SLAP_SCHERR_ATTR_NOT_FOUND;
505 if ( at->at_syntax_oid ) {
506 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
507 sat->sat_syntax = syn;
509 *err = sat->sat_syntax_oid;
510 return SLAP_SCHERR_SYN_NOT_FOUND;
512 if ( !strcmp(at->at_syntax_oid,
513 "1.3.6.1.4.1.1466.115.121.1.15") ) {
514 if ( at->at_equality_oid &&
515 !strcmp(at->at_equality_oid, "2.5.13.5") ) {
516 sat->sat_syntax_compat = SYNTAX_CES;
518 sat->sat_syntax_compat = SYNTAX_CIS;
520 } else if ( !strcmp(at->at_syntax_oid,
521 "1.3.6.1.4.1.1466.115.121.1.50") ) {
522 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
523 } else if ( !strcmp(at->at_syntax_oid,
524 "1.3.6.1.4.1.1466.115.121.1.12") ) {
525 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
526 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
527 sat->sat_syntax_compat = SYNTAX_BIN;
529 sat->sat_syntax_compat = DEFAULT_SYNTAX;
532 sat->sat_syntax_compat = DEFAULT_SYNTAX;
535 if ( sat->sat_equality_oid ) {
536 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
537 sat->sat_equality = mr;
539 *err = sat->sat_equality_oid;
540 return SLAP_SCHERR_MR_NOT_FOUND;
543 if ( sat->sat_ordering_oid ) {
544 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
545 sat->sat_ordering = mr;
547 *err = sat->sat_ordering_oid;
548 return SLAP_SCHERR_MR_NOT_FOUND;
551 if ( sat->sat_substr_oid ) {
552 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
553 sat->sat_substr = mr;
555 *err = sat->sat_substr_oid;
556 return SLAP_SCHERR_MR_NOT_FOUND;
561 * Now inherit definitions from superiors. We only check the
562 * direct superior since that one has already inherited from
565 if ( sat->sat_sup ) {
566 if ( !sat->sat_syntax ) {
567 sat->sat_syntax = sat->sat_sup->sat_syntax;
568 sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
570 if ( !sat->sat_equality ) {
571 sat->sat_equality = sat->sat_sup->sat_equality;
573 if ( !sat->sat_ordering ) {
574 sat->sat_ordering = sat->sat_sup->sat_ordering;
576 if ( !sat->sat_substr ) {
577 sat->sat_substr = sat->sat_sup->sat_substr;
580 code = at_insert(sat,err);
586 at_canonical_name( char * a_type )
590 if ( (atp=at_find(a_type)) == NULL ) {
594 } else if ( atp->sat_names
596 && (*(atp->sat_names[0]) != '\0') ) {
598 return atp->sat_names[0];
600 } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
610 }/* char * at_canonica_name() */
613 #if defined( SLAPD_SCHEMA_DN )
616 at_schema_info( Entry *e )
619 struct berval *vals[2];
625 for ( at = attr_list; at; at = at->sat_next ) {
626 val.bv_val = ldap_attributetype2str( &at->sat_atype );
628 val.bv_len = strlen( val.bv_val );
629 Debug( LDAP_DEBUG_TRACE, "Merging at [%d] %s\n",
630 val.bv_len, val.bv_val, 0 );
631 attr_merge( e, "attributeTypes", vals );
632 ldap_memfree( val.bv_val );
644 at_index_printnode( struct aindexrec *air )
647 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
652 at_index_print( void )
654 printf("Printing attribute type index:\n");
655 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
656 0, -1, AVL_INORDER );