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 )
42 for ( save = s; *s; s++ ) {
50 * attr_merge_fast - merge the given type and value with the list of
51 * attributes in attrs. called from str2entry(), where we can make some
52 * assumptions to make things faster.
53 * 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( 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
107 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
108 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
114 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
115 (*a)->a_type = attr_normalize( strdup( type ) );
117 (*a)->a_syntax = attr_syntax( type );
121 return( value_add( &(*a)->a_vals, vals ) );
125 * attr_find - find and return attribute type in list a
134 for ( ; a != NULL; a = a->a_next ) {
135 if ( strcasecmp( a->a_type, type ) == 0 ) {
144 * attr_delete - delete the attribute type in list pointed to by attrs
145 * return 0 deleted ok
146 * 1 not found in list a
147 * -1 something bad happened
159 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
160 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
176 #define DEFAULT_SYNTAX SYNTAX_CIS
183 static Avlnode *attr_syntaxes = NULL;
187 struct asyntaxinfo *a1,
188 struct asyntaxinfo *a2
191 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
195 attr_syntax_name_cmp(
197 struct asyntaxinfo *a
200 return( strcasecmp( type, a->asi_names[0] ) );
204 attr_syntax_names_cmp(
206 struct asyntaxinfo *a
211 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
212 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
221 struct asyntaxinfo *a1,
222 struct asyntaxinfo *a2
225 if ( a1->asi_syntax != a2->asi_syntax ) {
233 * attr_syntax - return the syntax of attribute type
237 attr_syntax( char *type )
239 struct asyntaxinfo *asi = NULL;
241 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
242 attr_syntax_name_cmp )) != NULL || (asi = (struct asyntaxinfo *)
243 avl_find_lin( attr_syntaxes, type, attr_syntax_names_cmp ))
246 return( asi->asi_syntax );
249 return( DEFAULT_SYNTAX );
253 * attr_syntax_config - process an attribute syntax config line
265 struct asyntaxinfo *a;
269 Debug( LDAP_DEBUG_ANY,
270 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
275 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
278 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
279 strcasecmp( argv[lasti], "cis" ) == 0 ) {
280 a->asi_syntax = SYNTAX_CIS;
281 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
282 strcasecmp( argv[lasti], "tel" ) == 0 ) {
283 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
284 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
285 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
286 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
287 strcasecmp( argv[lasti], "ces" ) == 0 ) {
288 a->asi_syntax = SYNTAX_CES;
289 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
290 strcasecmp( argv[lasti], "bin" ) == 0 ) {
291 a->asi_syntax = SYNTAX_BIN;
293 Debug( LDAP_DEBUG_ANY,
294 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
295 fname, lineno, argv[lasti] );
296 Debug( LDAP_DEBUG_ANY,
297 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
304 a->asi_names = charray_dup( argv );
307 switch ( avl_insert( &attr_syntaxes, (caddr_t) a, attr_syntax_cmp,
308 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, attr_syntax_printnode, 0, -1,