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>
26 attr_free( Attribute *a )
29 ber_bvecfree( a->a_vals );
34 attrs_free( Attribute *a )
38 for( ; a != NULL ; a = next ) {
44 Attribute *attr_dup( Attribute *a )
48 if( a == NULL) return NULL;
50 tmp = ch_malloc( sizeof(Attribute) );
52 if( a->a_vals != NULL ) {
55 for( i=0; a->a_vals[i] != NULL; i++ ) {
59 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
61 for( i=0; a->a_vals[i] != NULL; i++ ) {
62 tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
65 tmp->a_vals[i] = NULL;
71 tmp->a_type = ch_strdup( a->a_type );
72 tmp->a_syntax = a->a_syntax;
78 Attribute *attrs_dup( Attribute *a )
80 Attribute *tmp, **next;
82 if( a == NULL ) return NULL;
87 for( ; a != NULL ; a = a->a_next ) {
88 *next = attr_dup( a );
89 next = &((*next)->a_next);
97 * attr_normalize - normalize an attribute name (make it all lowercase)
101 attr_normalize( char *s )
105 for ( save = s; *s; s++ ) {
106 *s = TOLOWER( (unsigned char) *s );
113 * attr_merge_fast - merge the given type and value with the list of
114 * attributes in attrs. called from str2entry(), where we can make some
115 * assumptions to make things faster.
116 * returns 0 everything went ok
124 struct berval **vals,
134 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
135 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
142 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
143 (**a)->a_type = attr_normalize( ch_strdup( type ) );
144 (**a)->a_vals = NULL;
145 (**a)->a_syntax = attr_syntax( type );
146 (**a)->a_next = NULL;
149 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
154 * attr_merge - merge the given type and value with the list of
155 * attributes in attrs.
156 * returns 0 everything went ok
170 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
171 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
177 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
178 (*a)->a_type = attr_normalize( ch_strdup( type ) );
180 (*a)->a_syntax = attr_syntax( type );
184 return( value_add( &(*a)->a_vals, vals ) );
188 * attr_find - find and return attribute type in list a
197 for ( ; a != NULL; a = a->a_next ) {
198 if ( strcasecmp( a->a_type, type ) == 0 ) {
207 * attr_delete - delete the attribute type in list pointed to by attrs
208 * return 0 deleted ok
209 * 1 not found in list a
210 * -1 something bad happened
222 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
223 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
239 #define DEFAULT_SYNTAX SYNTAX_CIS
246 static Avlnode *attr_syntaxes = NULL;
250 struct asyntaxinfo *a1,
251 struct asyntaxinfo *a2
254 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
258 attr_syntax_name_cmp(
260 struct asyntaxinfo *a
263 return( strcasecmp( type, a->asi_names[0] ) );
267 attr_syntax_names_cmp(
269 struct asyntaxinfo *a
274 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
275 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
284 struct asyntaxinfo *a1,
285 struct asyntaxinfo *a2
288 if ( a1->asi_syntax != a2->asi_syntax ) {
296 * attr_syntax - return the syntax of attribute type
300 attr_syntax( char *type )
302 struct asyntaxinfo *asi = NULL;
304 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
305 attr_syntax_name_cmp )) != NULL || (asi = (struct asyntaxinfo *)
306 avl_find_lin( attr_syntaxes, type, attr_syntax_names_cmp ))
309 return( asi->asi_syntax );
312 return( DEFAULT_SYNTAX );
316 * attr_syntax_config - process an attribute syntax config line
328 struct asyntaxinfo *a;
332 Debug( LDAP_DEBUG_ANY,
333 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
338 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
341 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
342 strcasecmp( argv[lasti], "cis" ) == 0 ) {
343 a->asi_syntax = SYNTAX_CIS;
344 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
345 strcasecmp( argv[lasti], "tel" ) == 0 ) {
346 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
347 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
348 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
349 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
350 strcasecmp( argv[lasti], "ces" ) == 0 ) {
351 a->asi_syntax = SYNTAX_CES;
352 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
353 strcasecmp( argv[lasti], "bin" ) == 0 ) {
354 a->asi_syntax = SYNTAX_BIN;
356 Debug( LDAP_DEBUG_ANY,
357 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
358 fname, lineno, argv[lasti] );
359 Debug( LDAP_DEBUG_ANY,
360 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
367 a->asi_names = charray_dup( argv );
370 switch ( avl_insert( &attr_syntaxes, (caddr_t) a, attr_syntax_cmp,
371 attr_syntax_dup ) ) {
372 case -1: /* duplicate - different syntaxes */
373 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
377 case 1: /* duplicate - same syntaxes */
378 charray_free( a->asi_names );
382 default: /* inserted */
390 attr_syntax_printnode( struct asyntaxinfo *a )
394 printf( "syntax: 0x%x\n", a->asi_syntax );
395 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
396 printf( " name: %s\n", a->asi_names[i] );
402 attr_syntax_print( void )
404 (void) avl_apply( attr_syntaxes, attr_syntax_printnode, 0, -1,