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++ ) {
43 *s = TOLOWER( (unsigned char) *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
69 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
70 if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
77 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
78 (**a)->a_type = attr_normalize( ch_strdup( type ) );
80 (**a)->a_syntax = attr_syntax( type );
84 return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
89 * attr_merge - merge the given type and value with the list of
90 * attributes in attrs.
91 * returns 0 everything went ok
104 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
105 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
111 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
112 (*a)->a_type = attr_normalize( ch_strdup( type ) );
114 (*a)->a_syntax = attr_syntax( type );
118 return( value_add( &(*a)->a_vals, vals ) );
122 * attr_find - find and return attribute type in list a
131 for ( ; a != NULL; a = a->a_next ) {
132 if ( strcasecmp( a->a_type, type ) == 0 ) {
141 * attr_delete - delete the attribute type in list pointed to by attrs
142 * return 0 deleted ok
143 * 1 not found in list a
144 * -1 something bad happened
156 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
157 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
173 #define DEFAULT_SYNTAX SYNTAX_CIS
180 static Avlnode *attr_syntaxes = NULL;
184 struct asyntaxinfo *a1,
185 struct asyntaxinfo *a2
188 return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
192 attr_syntax_name_cmp(
194 struct asyntaxinfo *a
197 return( strcasecmp( type, a->asi_names[0] ) );
201 attr_syntax_names_cmp(
203 struct asyntaxinfo *a
208 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
209 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
218 struct asyntaxinfo *a1,
219 struct asyntaxinfo *a2
222 if ( a1->asi_syntax != a2->asi_syntax ) {
230 * attr_syntax - return the syntax of attribute type
234 attr_syntax( char *type )
236 struct asyntaxinfo *asi = NULL;
238 if ( (asi = (struct asyntaxinfo *) avl_find( attr_syntaxes, type,
239 (AVL_CMP) attr_syntax_name_cmp )) != NULL ||
240 (asi = (struct asyntaxinfo *) avl_find_lin( attr_syntaxes, type,
241 (AVL_CMP) attr_syntax_names_cmp )) != NULL )
243 return( asi->asi_syntax );
246 return( DEFAULT_SYNTAX );
250 * attr_syntax_config - process an attribute syntax config line
262 struct asyntaxinfo *a;
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
272 a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
275 if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
276 strcasecmp( argv[lasti], "cis" ) == 0 ) {
277 a->asi_syntax = SYNTAX_CIS;
278 } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
279 strcasecmp( argv[lasti], "tel" ) == 0 ) {
280 a->asi_syntax = (SYNTAX_CIS | SYNTAX_TEL);
281 } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
282 a->asi_syntax = (SYNTAX_CIS | SYNTAX_DN);
283 } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
284 strcasecmp( argv[lasti], "ces" ) == 0 ) {
285 a->asi_syntax = SYNTAX_CES;
286 } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
287 strcasecmp( argv[lasti], "bin" ) == 0 ) {
288 a->asi_syntax = SYNTAX_BIN;
290 Debug( LDAP_DEBUG_ANY,
291 "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
292 fname, lineno, argv[lasti] );
293 Debug( LDAP_DEBUG_ANY,
294 "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
301 a->asi_names = charray_dup( argv );
304 switch ( avl_insert( &attr_syntaxes, (caddr_t) a,
305 (AVL_CMP) attr_syntax_cmp,
306 (AVL_DUP) attr_syntax_dup ) ) {
307 case -1: /* duplicate - different syntaxes */
308 Debug( LDAP_DEBUG_ARGS, "%s: line %d: duplicate attribute\n",
312 case 1: /* duplicate - same syntaxes */
313 charray_free( a->asi_names );
317 default: /* inserted */
325 attr_syntax_printnode( struct asyntaxinfo *a )
329 printf( "syntax: 0x%x\n", a->asi_syntax );
330 for ( i = 0; a->asi_names[i] != NULL; i++ ) {
331 printf( " name: %s\n", a->asi_names[i] );
337 attr_syntax_print( void )
339 (void) avl_apply( attr_syntaxes, (AVL_APPLY) attr_syntax_printnode,
340 0, -1, AVL_INORDER );