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>
23 #include "ldapconfig.h"
27 static void at_index_print( void );
31 attr_free( Attribute *a )
34 ber_bvecfree( a->a_vals );
39 * attr_normalize - normalize an attribute name (make it all lowercase)
43 attr_normalize( char *s )
49 for ( save = s; *s; s++ ) {
50 *s = TOLOWER( (unsigned char) *s );
57 * attr_merge_fast - merge the given type and value with the list of
58 * attributes in attrs. called from str2entry(), where we can make some
59 * assumptions to make things faster.
60 * returns 0 everything went ok
76 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
77 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
84 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
85 (**a)->a_type = attr_normalize( ch_strdup( type ) );
87 (**a)->a_syntax = attr_syntax( type );
91 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
96 * attr_merge - merge the given type and value with the list of
97 * attributes in attrs.
98 * returns 0 everything went ok
111 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
112 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
118 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
119 (*a)->a_type = attr_normalize( ch_strdup( type ) );
121 (*a)->a_syntax = attr_syntax( type );
125 return( value_add( &(*a)->a_vals, vals ) );
129 * attr_find - find and return attribute type in list a
138 for ( ; a != NULL; a = a->a_next ) {
139 if ( strcasecmp( a->a_type, type ) == 0 ) {
148 * attr_delete - delete the attribute type in list pointed to by attrs
149 * return 0 deleted ok
150 * 1 not found in list a
151 * -1 something bad happened
163 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
164 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
180 #define DEFAULT_SYNTAX SYNTAX_CIS
183 * attr_syntax - return the syntax of attribute type
187 attr_syntax( char *type )
193 return( sat->sat_syntax_compat );
196 return( DEFAULT_SYNTAX );
200 * attr_syntax_config - process an attribute syntax config line
212 LDAP_ATTRIBUTE_TYPE *at;
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
224 at = (LDAP_ATTRIBUTE_TYPE *)
225 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
228 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
229 strcasecmp( argv[lasti], "cis" ) == 0 ) {
230 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
231 at->at_equality_oid = "2.5.13.2";
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 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
237 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
238 at->at_equality_oid = "2.5.13.1";
239 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
240 strcasecmp( argv[lasti], "ces" ) == 0 ) {
241 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
242 /* notice: this is caseExactIA5Match */
243 at->at_equality_oid = "1.3.6.1.4.1.1466.109.114.1";
244 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
245 strcasecmp( argv[lasti], "bin" ) == 0 ) {
246 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
247 /* There is no match for binary syntax. Really */
249 Debug( LDAP_DEBUG_ANY,
250 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
251 fname, lineno, argv[lasti] );
252 Debug( LDAP_DEBUG_ANY,
253 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
255 free( (AttributeType *) at );
261 at->at_names = charray_dup( argv );
264 code = at_add( at, &err );
266 fprintf( stderr, "%s: line %d: %s %s\n",
267 fname, lineno, scherr2str(code), err);
280 if ( at_find( name ) ) {
286 attr_syntax_config( "implicit", 0, 2, argv );
293 AttributeType *air_at;
296 static Avlnode *attr_index = NULL;
297 static AttributeType *attr_list = NULL;
301 struct aindexrec *air1,
302 struct aindexrec *air2
305 return (strcasecmp( air1->air_name, air2->air_name ));
311 struct aindexrec *air
314 return (strcasecmp( type, air->air_name ));
322 struct aindexrec *air = NULL;
324 if ( (air = (struct aindexrec *) avl_find( attr_index, name,
325 (AVL_CMP) attr_index_name_cmp )) != NULL ) {
326 return( air->air_at );
334 AttributeType ***listp
337 AttributeType **list;
338 AttributeType **list1;
344 list = calloc(size, sizeof(AttributeType *));
356 list1 = realloc(list, size*sizeof(AttributeType *));
371 AttributeType ***listp
374 AttributeType **list;
375 AttributeType **list1;
383 for ( i=0; list[i]; i++ )
388 for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
392 /* Tell the runtime this can be shrinked */
393 list1 = realloc(list, (i+1)*sizeof(AttributeType **));
412 for ( i=0; list[i]; i++ ) {
413 if ( sat == list[i] ) {
427 struct aindexrec *air;
431 while ( *atp != NULL ) {
432 atp = &(*atp)->sat_next;
436 if ( sat->sat_oid ) {
437 air = (struct aindexrec *)
438 ch_calloc( 1, sizeof(struct aindexrec) );
439 air->air_name = sat->sat_oid;
441 if ( avl_insert( &attr_index, (caddr_t) air,
442 (AVL_CMP) attr_index_cmp,
443 (AVL_DUP) avl_dup_error ) ) {
446 return SLAP_SCHERR_DUP_ATTR;
448 /* FIX: temporal consistency check */
449 at_find(air->air_name);
451 if ( (names = sat->sat_names) ) {
453 air = (struct aindexrec *)
454 ch_calloc( 1, sizeof(struct aindexrec) );
455 air->air_name = ch_strdup(*names);
457 if ( avl_insert( &attr_index, (caddr_t) air,
458 (AVL_CMP) attr_index_cmp,
459 (AVL_DUP) avl_dup_error ) ) {
462 return SLAP_SCHERR_DUP_ATTR;
464 /* FIX: temporal consistency check */
465 at_find(air->air_name);
475 LDAP_ATTRIBUTE_TYPE *at,
484 if ( at->at_names && at->at_names[0] ) {
485 errattr = at->at_names[0];
487 errattr = at->at_oid;
489 sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
490 memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
491 if ( at->at_sup_oid ) {
492 if ( (sat1 = at_find(at->at_sup_oid)) ) {
494 if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
496 return SLAP_SCHERR_OUTOFMEM;
499 *err = at->at_sup_oid;
500 return SLAP_SCHERR_ATTR_NOT_FOUND;
504 if ( at->at_syntax_oid ) {
505 if ( !strcmp(at->at_syntax_oid,
506 "1.3.6.1.4.1.1466.115.121.1.15") ) {
507 if ( at->at_equality_oid &&
508 !strcmp(at->at_equality_oid,
509 "1.3.6.1.4.1.1466.109.114.1") ) {
510 sat->sat_syntax_compat = SYNTAX_CES;
512 sat->sat_syntax_compat = SYNTAX_CIS;
514 } else if ( !strcmp(at->at_syntax_oid,
515 "1.3.6.1.4.1.1466.115.121.1.50") ) {
516 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
517 } else if ( !strcmp(at->at_syntax_oid,
518 "1.3.6.1.4.1.1466.115.121.1.12") ) {
519 sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
520 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
521 sat->sat_syntax_compat = SYNTAX_BIN;
523 sat->sat_syntax_compat = DEFAULT_SYNTAX;
526 sat->sat_syntax_compat = DEFAULT_SYNTAX;
529 code = at_insert(sat,err);
533 #if defined( SLAPD_SCHEMA_DN )
536 at_schema_info( Entry *e )
539 struct berval *vals[2];
545 for ( at = attr_list; at; at = at->sat_next ) {
546 val.bv_val = ldap_attributetype2str( &at->sat_atype );
548 val.bv_len = strlen( val.bv_val );
549 Debug( LDAP_DEBUG_TRACE, "Merging at [%d] %s\n",
550 val.bv_len, val.bv_val, 0 );
551 attr_merge( e, "attributeTypes", vals );
552 ldap_memfree( val.bv_val );
564 at_index_printnode( struct aindexrec *air )
567 printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
572 at_index_print( void )
574 printf("Printing attribute type index:\n");
575 (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
576 0, -1, AVL_INORDER );