1 /* attr.c - routines for dealing with attributes */
12 #include <ac/socket.h>
13 #include <ac/string.h>
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
24 extern char **charray_dup();
28 attr_free( Attribute *a )
31 ber_bvecfree( a->a_vals );
36 * attr_normalize - normalize an attribute name (make it all lowercase)
40 attr_normalize( char *s )
44 for ( save = s; *s; 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
73 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
74 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
81 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
82 (**a)->a_type = attr_normalize( strdup( type ) );
84 (**a)->a_syntax = attr_syntax( type );
88 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
93 * attr_merge - merge the given type and value with the list of
94 * attributes in attrs.
95 * returns 0 everything went ok
109 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
110 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
116 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
117 (*a)->a_type = attr_normalize( strdup( type ) );
119 (*a)->a_syntax = attr_syntax( type );
123 return( value_add( &(*a)->a_vals, vals ) );
127 * attr_find - find and return attribute type in list a
136 for ( ; a != NULL; a = a->a_next ) {
137 if ( strcasecmp( a->a_type, type ) == 0 ) {
146 * attr_delete - delete the attribute type in list pointed to by attrs
147 * return 0 deleted ok
148 * 1 not found in list a
149 * -1 something bad happened
161 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
162 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
178 #define DEFAULT_SYNTAX SYNTAX_CIS
185 static Avlnode *attr_syntaxes = NULL;
189 struct asyntaxinfo *a1,
190 struct asyntaxinfo *a2
193 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
197 attr_syntax_name_cmp(
199 struct asyntaxinfo *a
202 return( strcasecmp( type, a->asi_names[0] ) );
206 attr_syntax_names_cmp(
208 struct asyntaxinfo *a
213 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
214 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
223 struct asyntaxinfo *a1,
224 struct asyntaxinfo *a2
227 if ( a1->asi_syntax != a2->asi_syntax ) {
235 * attr_syntax - return the syntax of attribute type
239 attr_syntax( char *type )
241 struct asyntaxinfo *asi = NULL;
243 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
244 attr_syntax_name_cmp )) != NULL || (asi = (struct asyntaxinfo *)
245 avl_find_lin( attr_syntaxes, type, attr_syntax_names_cmp ))
248 return( asi->asi_syntax );
251 return( DEFAULT_SYNTAX );
255 * attr_syntax_config - process an attribute syntax config line
267 struct asyntaxinfo *a;
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
277 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
280 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
281 strcasecmp( argv[lasti], "cis" ) == 0 ) {
282 a->asi_syntax = SYNTAX_CIS;
283 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
284 strcasecmp( argv[lasti], "tel" ) == 0 ) {
285 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
286 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
287 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
288 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
289 strcasecmp( argv[lasti], "ces" ) == 0 ) {
290 a->asi_syntax = SYNTAX_CES;
291 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
292 strcasecmp( argv[lasti], "bin" ) == 0 ) {
293 a->asi_syntax = SYNTAX_BIN;
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
298 Debug( LDAP_DEBUG_ANY,
299 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
306 a->asi_names = charray_dup( argv );
309 switch ( avl_insert( &attr_syntaxes, (caddr_t) a, attr_syntax_cmp,
310 attr_syntax_dup ) ) {
311 case -1: /* duplicate - different syntaxes */
312 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
316 case 1: /* duplicate - same syntaxes */
317 charray_free( a->asi_names );
321 default: /* inserted */
329 attr_syntax_printnode( struct asyntaxinfo *a )
333 printf( "syntax: 0x%x\n", a->asi_syntax );
334 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
335 printf( " name: %s\n", a->asi_names[i] );
343 (void) avl_apply( attr_syntaxes, attr_syntax_printnode, 0, -1,