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 * attr_normalize - normalize an attribute name (make it all lowercase)
38 attr_normalize( char *s )
44 for ( save = s; *s; s++ ) {
45 *s = TOLOWER( (unsigned char) *s );
52 * attr_merge_fast - merge the given type and value with the list of
53 * attributes in attrs. called from str2entry(), where we can make some
54 * assumptions to make things faster.
55 * returns 0 everything went ok
71 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
72 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
79 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
80 (**a)->a_type = attr_normalize( ch_strdup( type ) );
82 (**a)->a_syntax = attr_syntax( type );
86 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
91 * attr_merge - merge the given type and value with the list of
92 * attributes in attrs.
93 * returns 0 everything went ok
106 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
107 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
113 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
114 (*a)->a_type = attr_normalize( ch_strdup( type ) );
116 (*a)->a_syntax = attr_syntax( type );
120 return( value_add( &(*a)->a_vals, vals ) );
124 * attr_find - find and return attribute type in list a
133 for ( ; a != NULL; a = a->a_next ) {
134 if ( strcasecmp( a->a_type, type ) == 0 ) {
143 * attr_delete - delete the attribute type in list pointed to by attrs
144 * return 0 deleted ok
145 * 1 not found in list a
146 * -1 something bad happened
158 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
159 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
175 #define DEFAULT_SYNTAX SYNTAX_CIS
182 static Avlnode *attr_syntaxes = NULL;
186 struct asyntaxinfo *a1,
187 struct asyntaxinfo *a2
190 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
194 attr_syntax_name_cmp(
196 struct asyntaxinfo *a
199 return( strcasecmp( type, a->asi_names[0] ) );
203 attr_syntax_names_cmp(
205 struct asyntaxinfo *a
210 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
211 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
220 struct asyntaxinfo *a1,
221 struct asyntaxinfo *a2
224 if ( a1->asi_syntax != a2->asi_syntax ) {
232 * attr_syntax - return the syntax of attribute type
236 attr_syntax( char *type )
238 struct asyntaxinfo *asi = NULL;
240 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
241 (AVL_CMP) attr_syntax_name_cmp )) != NULL ||
242 (asi = (struct asyntaxinfo *) avl_find_lin( attr_syntaxes, type,
243 (AVL_CMP) attr_syntax_names_cmp )) != NULL )
245 return( asi->asi_syntax );
248 return( DEFAULT_SYNTAX );
252 * attr_syntax_config - process an attribute syntax config line
264 struct asyntaxinfo *a;
268 Debug( LDAP_DEBUG_ANY,
269 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
274 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
277 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
278 strcasecmp( argv[lasti], "cis" ) == 0 ) {
279 a->asi_syntax = SYNTAX_CIS;
280 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
281 strcasecmp( argv[lasti], "tel" ) == 0 ) {
282 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
283 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
284 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
285 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
286 strcasecmp( argv[lasti], "ces" ) == 0 ) {
287 a->asi_syntax = SYNTAX_CES;
288 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
289 strcasecmp( argv[lasti], "bin" ) == 0 ) {
290 a->asi_syntax = SYNTAX_BIN;
292 Debug( LDAP_DEBUG_ANY,
293 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
294 fname, lineno, argv[lasti] );
295 Debug( LDAP_DEBUG_ANY,
296 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
303 a->asi_names = charray_dup( argv );
306 switch ( avl_insert( &attr_syntaxes, (caddr_t) a,
307 (AVL_CMP) attr_syntax_cmp,
308 (AVL_DUP) attr_syntax_dup ) ) {
309 case -1: /* duplicate - different syntaxes */
310 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
314 case 1: /* duplicate - same syntaxes */
315 charray_free( a->asi_names );
319 default: /* inserted */
327 attr_syntax_printnode( struct asyntaxinfo *a )
331 printf( "syntax: 0x%x\n", a->asi_syntax );
332 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
333 printf( " name: %s\n", a->asi_names[i] );
339 attr_syntax_print( void )
341 (void) avl_apply( attr_syntaxes, (AVL_APPLY) attr_syntax_printnode,
342 0, -1, AVL_INORDER );