1 /* attr.c - routines for dealing with attributes */
9 #include <sys/socket.h>
10 #include <sys/param.h>
15 extern char **charray_dup();
16 extern char *ch_malloc();
20 attr_free( Attribute *a )
23 ber_bvecfree( a->a_vals );
28 * attr_normalize - normalize an attribute name (make it all lowercase)
32 attr_normalize( char *s )
36 for ( save = s; *s; s++ ) {
44 * attr_merge_fast - merge the given type and value with the list of
45 * attributes in attrs. called from str2entry(), where we can make some
46 * assumptions to make things faster.
47 * returns 0 everything went ok
65 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
66 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
73 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
74 (**a)->a_type = attr_normalize( strdup( type ) );
76 (**a)->a_syntax = attr_syntax( type );
80 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
85 * attr_merge - merge the given type and value with the list of
86 * attributes in attrs.
87 * returns 0 everything went ok
101 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
102 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
108 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
109 (*a)->a_type = attr_normalize( strdup( type ) );
111 (*a)->a_syntax = attr_syntax( type );
115 return( value_add( &(*a)->a_vals, vals ) );
119 * attr_find - find and return attribute type in list a
128 for ( ; a != NULL; a = a->a_next ) {
129 if ( strcasecmp( a->a_type, type ) == 0 ) {
138 * attr_delete - delete the attribute type in list pointed to by attrs
139 * return 0 deleted ok
140 * 1 not found in list a
141 * -1 something bad happened
153 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
154 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
170 #define DEFAULT_SYNTAX SYNTAX_CIS
177 static Avlnode *attr_syntaxes = NULL;
181 struct asyntaxinfo *a1,
182 struct asyntaxinfo *a2
185 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
189 attr_syntax_name_cmp(
191 struct asyntaxinfo *a
194 return( strcasecmp( type, a->asi_names[0] ) );
198 attr_syntax_names_cmp(
200 struct asyntaxinfo *a
205 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
206 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
215 struct asyntaxinfo *a1,
216 struct asyntaxinfo *a2
219 if ( a1->asi_syntax != a2->asi_syntax ) {
227 * attr_syntax - return the syntax of attribute type
231 attr_syntax( char *type )
233 struct asyntaxinfo *asi = NULL;
235 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
236 attr_syntax_name_cmp )) != NULL || (asi = (struct asyntaxinfo *)
237 avl_find_lin( attr_syntaxes, type, attr_syntax_names_cmp ))
240 return( asi->asi_syntax );
243 return( DEFAULT_SYNTAX );
247 * attr_syntax_config - process an attribute syntax config line
259 struct asyntaxinfo *a;
263 Debug( LDAP_DEBUG_ANY,
264 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
269 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
272 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
273 strcasecmp( argv[lasti], "cis" ) == 0 ) {
274 a->asi_syntax = SYNTAX_CIS;
275 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
276 strcasecmp( argv[lasti], "tel" ) == 0 ) {
277 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
278 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
279 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
280 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
281 strcasecmp( argv[lasti], "ces" ) == 0 ) {
282 a->asi_syntax = SYNTAX_CES;
283 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
284 strcasecmp( argv[lasti], "bin" ) == 0 ) {
285 a->asi_syntax = SYNTAX_BIN;
287 Debug( LDAP_DEBUG_ANY,
288 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
290 Debug( LDAP_DEBUG_ANY,
291 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
298 a->asi_names = charray_dup( argv );
301 switch ( avl_insert( &attr_syntaxes, a, attr_syntax_cmp,
302 attr_syntax_dup ) ) {
303 case -1: /* duplicate - different syntaxes */
304 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
308 case 1: /* duplicate - same syntaxes */
309 charray_free( a->asi_names );
313 default: /* inserted */
321 attr_syntax_printnode( struct asyntaxinfo *a )
325 printf( "syntax: 0x%x\n", a->asi_syntax );
326 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
327 printf( " name: %s\n", a->asi_names[i] );
335 (void) avl_apply( attr_syntaxes, attr_syntax_printnode, 0, -1,