1 /* attr.c - routines for dealing with attributes */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/param.h>
17 extern char **charray_dup();
18 extern char *ch_malloc();
22 attr_free( Attribute *a )
25 ber_bvecfree( a->a_vals );
30 * attr_normalize - normalize an attribute name (make it all lowercase)
34 attr_normalize( char *s )
38 for ( save = s; *s; s++ ) {
46 * attr_merge_fast - merge the given type and value with the list of
47 * attributes in attrs. called from str2entry(), where we can make some
48 * assumptions to make things faster.
49 * returns 0 everything went ok
67 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
68 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
75 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
76 (**a)->a_type = attr_normalize( strdup( type ) );
78 (**a)->a_syntax = attr_syntax( type );
82 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
87 * attr_merge - merge the given type and value with the list of
88 * attributes in attrs.
89 * 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( 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
179 static Avlnode *attr_syntaxes = NULL;
183 struct asyntaxinfo *a1,
184 struct asyntaxinfo *a2
187 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
191 attr_syntax_name_cmp(
193 struct asyntaxinfo *a
196 return( strcasecmp( type, a->asi_names[0] ) );
200 attr_syntax_names_cmp(
202 struct asyntaxinfo *a
207 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
208 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
217 struct asyntaxinfo *a1,
218 struct asyntaxinfo *a2
221 if ( a1->asi_syntax != a2->asi_syntax ) {
229 * attr_syntax - return the syntax of attribute type
233 attr_syntax( char *type )
235 struct asyntaxinfo *asi = NULL;
237 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
238 attr_syntax_name_cmp )) != NULL || (asi = (struct asyntaxinfo *)
239 avl_find_lin( attr_syntaxes, type, attr_syntax_names_cmp ))
242 return( asi->asi_syntax );
245 return( DEFAULT_SYNTAX );
249 * attr_syntax_config - process an attribute syntax config line
261 struct asyntaxinfo *a;
265 Debug( LDAP_DEBUG_ANY,
266 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
271 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
274 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
275 strcasecmp( argv[lasti], "cis" ) == 0 ) {
276 a->asi_syntax = SYNTAX_CIS;
277 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
278 strcasecmp( argv[lasti], "tel" ) == 0 ) {
279 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
280 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
281 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
282 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
283 strcasecmp( argv[lasti], "ces" ) == 0 ) {
284 a->asi_syntax = SYNTAX_CES;
285 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
286 strcasecmp( argv[lasti], "bin" ) == 0 ) {
287 a->asi_syntax = SYNTAX_BIN;
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
292 Debug( LDAP_DEBUG_ANY,
293 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
300 a->asi_names = charray_dup( argv );
303 switch ( avl_insert( &attr_syntaxes, (caddr_t) a, attr_syntax_cmp,
304 attr_syntax_dup ) ) {
305 case -1: /* duplicate - different syntaxes */
306 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
310 case 1: /* duplicate - same syntaxes */
311 charray_free( a->asi_names );
315 default: /* inserted */
323 attr_syntax_printnode( struct asyntaxinfo *a )
327 printf( "syntax: 0x%x\n", a->asi_syntax );
328 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
329 printf( " name: %s\n", a->asi_names[i] );
337 (void) avl_apply( attr_syntaxes, attr_syntax_printnode, 0, -1,