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>
27 attr_free( Attribute *a )
30 ber_bvecfree( a->a_vals );
35 * attr_normalize - normalize an attribute name (make it all lowercase)
39 attr_normalize( char *s )
43 for ( save = s; *s; s++ ) {
51 * attr_merge_fast - merge the given type and value with the list of
52 * attributes in attrs. called from str2entry(), where we can make some
53 * assumptions to make things faster.
54 * returns 0 everything went ok
72 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
73 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
80 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
81 (**a)->a_type = attr_normalize( strdup( type ) );
83 (**a)->a_syntax = attr_syntax( type );
87 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
92 * attr_merge - merge the given type and value with the list of
93 * attributes in attrs.
94 * returns 0 everything went ok
108 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
109 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
115 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
116 (*a)->a_type = attr_normalize( strdup( type ) );
118 (*a)->a_syntax = attr_syntax( type );
122 return( value_add( &(*a)->a_vals, vals ) );
126 * attr_find - find and return attribute type in list a
135 for ( ; a != NULL; a = a->a_next ) {
136 if ( strcasecmp( a->a_type, type ) == 0 ) {
145 * attr_delete - delete the attribute type in list pointed to by attrs
146 * return 0 deleted ok
147 * 1 not found in list a
148 * -1 something bad happened
160 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
161 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
177 #define DEFAULT_SYNTAX SYNTAX_CIS
184 static Avlnode *attr_syntaxes = NULL;
188 struct asyntaxinfo *a1,
189 struct asyntaxinfo *a2
192 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
196 attr_syntax_name_cmp(
198 struct asyntaxinfo *a
201 return( strcasecmp( type, a->asi_names[0] ) );
205 attr_syntax_names_cmp(
207 struct asyntaxinfo *a
212 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
213 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
222 struct asyntaxinfo *a1,
223 struct asyntaxinfo *a2
226 if ( a1->asi_syntax != a2->asi_syntax ) {
234 * attr_syntax - return the syntax of attribute type
238 attr_syntax( char *type )
240 struct asyntaxinfo *asi = NULL;
242 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
243 attr_syntax_name_cmp )) != NULL || (asi = (struct asyntaxinfo *)
244 avl_find_lin( attr_syntaxes, type, attr_syntax_names_cmp ))
247 return( asi->asi_syntax );
250 return( DEFAULT_SYNTAX );
254 * attr_syntax_config - process an attribute syntax config line
266 struct asyntaxinfo *a;
270 Debug( LDAP_DEBUG_ANY,
271 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
276 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
279 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
280 strcasecmp( argv[lasti], "cis" ) == 0 ) {
281 a->asi_syntax = SYNTAX_CIS;
282 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
283 strcasecmp( argv[lasti], "tel" ) == 0 ) {
284 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
285 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
286 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
287 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
288 strcasecmp( argv[lasti], "ces" ) == 0 ) {
289 a->asi_syntax = SYNTAX_CES;
290 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
291 strcasecmp( argv[lasti], "bin" ) == 0 ) {
292 a->asi_syntax = SYNTAX_BIN;
294 Debug( LDAP_DEBUG_ANY,
295 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
296 fname, lineno, argv[lasti] );
297 Debug( LDAP_DEBUG_ANY,
298 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
305 a->asi_names = charray_dup( argv );
308 switch ( avl_insert( &attr_syntaxes, (caddr_t) a, attr_syntax_cmp,
309 attr_syntax_dup ) ) {
310 case -1: /* duplicate - different syntaxes */
311 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
315 case 1: /* duplicate - same syntaxes */
316 charray_free( a->asi_names );
320 default: /* inserted */
328 attr_syntax_printnode( struct asyntaxinfo *a )
332 printf( "syntax: 0x%x\n", a->asi_syntax );
333 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
334 printf( " name: %s\n", a->asi_names[i] );
340 attr_syntax_print( void )
342 (void) avl_apply( attr_syntaxes, attr_syntax_printnode, 0, -1,