]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
e4fa388fa99b6a0f32b66d89f7de0b70e2e2ffb9
[openldap] / servers / slapd / attr.c
1 /* attr.c - routines for dealing with attributes */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #ifdef HAVE_FCNTL_H
8 #include <fcntl.h>
9 #endif
10
11 #include <ac/ctype.h>
12 #include <ac/socket.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
18 #endif
19
20 #include <sys/stat.h>
21
22 #include "slap.h"
23
24 extern char     **charray_dup();
25 extern int      errno;
26
27 void
28 attr_free( Attribute *a )
29 {
30         free( a->a_type );
31         ber_bvecfree( a->a_vals );
32         free( a );
33 }
34
35 /*
36  * attr_normalize - normalize an attribute name (make it all lowercase)
37  */
38
39 char *
40 attr_normalize( char *s )
41 {
42         char    *save;
43
44         for ( save = s; *s; s++ ) {
45                 *s = TOLOWER( *s );
46         }
47
48         return( save );
49 }
50
51 /*
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
56  *              -1      trouble
57  */
58
59 int
60 attr_merge_fast(
61     Entry               *e,
62     char                *type,
63     struct berval       **vals,
64     int                 nvals,
65     int                 naddvals,
66     int                 *maxvals,
67     Attribute           ***a
68 )
69 {
70         int             i;
71
72         if ( *a == NULL ) {
73                 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
74                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
75                                 break;
76                         }
77                 }
78         }
79
80         if ( **a == NULL ) {
81                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
82                 (**a)->a_type = attr_normalize( strdup( type ) );
83                 (**a)->a_vals = NULL;
84                 (**a)->a_syntax = attr_syntax( type );
85                 (**a)->a_next = NULL;
86         }
87
88         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
89             maxvals ) );
90 }
91
92 /*
93  * attr_merge - merge the given type and value with the list of
94  * attributes in attrs.
95  * returns      0       everything went ok
96  *              -1      trouble
97  */
98
99 int
100 attr_merge(
101     Entry               *e,
102     char                *type,
103     struct berval       **vals
104 )
105 {
106         int             i;
107         Attribute       **a;
108
109         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
110                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
111                         break;
112                 }
113         }
114
115         if ( *a == NULL ) {
116                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
117                 (*a)->a_type = attr_normalize( strdup( type ) );
118                 (*a)->a_vals = NULL;
119                 (*a)->a_syntax = attr_syntax( type );
120                 (*a)->a_next = NULL;
121         }
122
123         return( value_add( &(*a)->a_vals, vals ) );
124 }
125
126 /*
127  * attr_find - find and return attribute type in list a
128  */
129
130 Attribute *
131 attr_find(
132     Attribute   *a,
133     char        *type
134 )
135 {
136         for ( ; a != NULL; a = a->a_next ) {
137                 if ( strcasecmp( a->a_type, type ) == 0 ) {
138                         return( a );
139                 }
140         }
141
142         return( NULL );
143 }
144
145 /*
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
150  */
151
152 int
153 attr_delete(
154     Attribute   **attrs,
155     char        *type
156 )
157 {
158         Attribute       **a;
159         Attribute       *save;
160
161         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
162                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
163                         break;
164                 }
165         }
166
167         if ( *a == NULL ) {
168                 return( 1 );
169         }
170
171         save = *a;
172         *a = (*a)->a_next;
173         attr_free( save );
174
175         return( 0 );
176 }
177
178 #define DEFAULT_SYNTAX  SYNTAX_CIS
179
180 struct asyntaxinfo {
181         char    **asi_names;
182         int     asi_syntax;
183 };
184
185 static Avlnode  *attr_syntaxes = NULL;
186
187 static int
188 attr_syntax_cmp(
189     struct asyntaxinfo        *a1,
190     struct asyntaxinfo        *a2
191 )
192 {
193       return( strcasecmp( a1->asi_names[0], a2->asi_names[0] ) );
194 }
195
196 static int
197 attr_syntax_name_cmp(
198     char                *type,
199     struct asyntaxinfo  *a
200 )
201 {
202         return( strcasecmp( type, a->asi_names[0] ) );
203 }
204
205 static int
206 attr_syntax_names_cmp(
207     char                *type,
208     struct asyntaxinfo  *a
209 )
210 {
211         int     i;
212
213         for ( i = 0; a->asi_names[i] != NULL; i++ ) {
214                 if ( strcasecmp( type, a->asi_names[i] ) == 0 ) {
215                         return( 0 );
216                 }
217         }
218         return( 1 );
219 }
220
221 static int
222 attr_syntax_dup(
223     struct asyntaxinfo        *a1,
224     struct asyntaxinfo        *a2
225 )
226 {
227         if ( a1->asi_syntax != a2->asi_syntax ) {
228                 return( -1 );
229         }
230
231         return( 1 );
232 }
233
234 /*
235  * attr_syntax - return the syntax of attribute type
236  */
237
238 int
239 attr_syntax( char *type )
240 {
241         struct asyntaxinfo      *asi = NULL;
242
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 ))
246             != NULL )
247         {
248                 return( asi->asi_syntax );
249         }
250
251         return( DEFAULT_SYNTAX );
252 }
253
254 /*
255  * attr_syntax_config - process an attribute syntax config line
256  */
257
258 void
259 attr_syntax_config(
260     char        *fname,
261     int         lineno,
262     int         argc,
263     char        **argv
264 )
265 {
266         char                    *save;
267         struct asyntaxinfo      *a;
268         int                     i, lasti;
269
270         if ( argc < 2 ) {
271                 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
273                     fname, lineno, 0 );
274                 return;
275         }
276
277         a = (struct asyntaxinfo *) ch_calloc( 1, sizeof(struct asyntaxinfo) );
278
279         lasti = argc - 1;
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;
294         } else {
295                 Debug( LDAP_DEBUG_ANY,
296             "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
297                     fname, lineno, 0 );
298                 Debug( LDAP_DEBUG_ANY,
299     "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
300                     0, 0, 0 );
301                 free( (char *) a );
302                 return;
303         }
304         save = argv[lasti];
305         argv[lasti] = NULL;
306         a->asi_names = charray_dup( argv );
307         argv[lasti] = save;
308
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",
313                     fname, lineno, 0 );
314                 /* FALL */
315
316         case 1:         /* duplicate - same syntaxes */
317                 charray_free( a->asi_names );
318                 free( (char *) a );
319                 break;
320
321         default:        /* inserted */
322                 break;
323         }
324 }
325
326 #ifdef LDAP_DEBUG
327
328 static
329 attr_syntax_printnode( struct asyntaxinfo *a )
330 {
331         int     i;
332
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] );
336         }
337         return( 0 );
338 }
339
340 static
341 attr_syntax_print()
342 {
343         (void) avl_apply( attr_syntaxes, attr_syntax_printnode, 0, -1,
344             AVL_INORDER );
345 }
346
347 #endif