]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attr.c
ITS#4040 move initialization
[openldap] / servers / slapd / back-bdb / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-bdb.h"
26 #include "lutil.h"
27
28
29 static int
30 ainfo_type_cmp(
31         const void *v_desc,
32         const void *v_a
33 )
34 {
35         const AttributeDescription *desc = v_desc;
36         const AttrInfo  *a = v_a;
37         return SLAP_PTRCMP(desc, a->ai_desc);
38 }
39
40 static int
41 ainfo_cmp(
42         const void      *v_a,
43         const void      *v_b
44 )
45 {
46         const AttrInfo *a = v_a, *b = v_b;
47         return SLAP_PTRCMP(a->ai_desc, b->ai_desc);
48 }
49
50 AttrInfo *
51 bdb_attr_mask(
52         struct bdb_info *bdb,
53         AttributeDescription *desc )
54 {
55
56         return avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
57 }
58
59 int
60 bdb_attr_index_config(
61         struct bdb_info *bdb,
62         const char              *fname,
63         int                     lineno,
64         int                     argc,
65         char            **argv )
66 {
67         int rc;
68         int     i;
69         slap_mask_t mask;
70         char **attrs;
71         char **indexes = NULL;
72
73         attrs = ldap_str2charray( argv[0], "," );
74
75         if( attrs == NULL ) {
76                 fprintf( stderr, "%s: line %d: "
77                         "no attributes specified: %s\n",
78                         fname, lineno, argv[0] );
79                 return LDAP_PARAM_ERROR;
80         }
81
82         if ( argc > 1 ) {
83                 indexes = ldap_str2charray( argv[1], "," );
84
85                 if( indexes == NULL ) {
86                         fprintf( stderr, "%s: line %d: "
87                                 "no indexes specified: %s\n",
88                                 fname, lineno, argv[1] );
89                         return LDAP_PARAM_ERROR;
90                 }
91         }
92
93         if( indexes == NULL ) {
94                 mask = bdb->bi_defaultmask;
95
96         } else {
97                 mask = 0;
98
99                 for ( i = 0; indexes[i] != NULL; i++ ) {
100                         slap_mask_t index;
101                         rc = slap_str2index( indexes[i], &index );
102
103                         if( rc != LDAP_SUCCESS ) {
104                                 fprintf( stderr, "%s: line %d: "
105                                         "index type \"%s\" undefined\n",
106                                         fname, lineno, indexes[i] );
107                                 return LDAP_PARAM_ERROR;
108                         }
109
110                         mask |= index;
111                 }
112         }
113
114         if( !mask ) {
115                 fprintf( stderr, "%s: line %d: "
116                         "no indexes selected\n",
117                         fname, lineno );
118                 return LDAP_PARAM_ERROR;
119         }
120
121         for ( i = 0; attrs[i] != NULL; i++ ) {
122                 AttrInfo        *a;
123                 AttributeDescription *ad;
124                 const char *text;
125 #ifdef LDAP_COMP_MATCH
126                 ComponentReference* cr = NULL;
127                 AttrInfo *a_cr = NULL;
128 #endif
129
130                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
131                         bdb->bi_defaultmask |= mask;
132                         continue;
133                 }
134
135 #ifdef LDAP_COMP_MATCH
136                 if ( is_component_reference( attrs[i] ) ) {
137                         rc = extract_component_reference( attrs[i], &cr );
138                         if ( rc != LDAP_SUCCESS ) {
139                                 fprintf( stderr, "%s: line %d: "
140                                         "index component reference\"%s\" undefined\n",
141                                         fname, lineno, attrs[i] );
142                                 return rc;
143                         }
144                         cr->cr_indexmask = mask;
145                         /*
146                          * After extracting a component reference
147                          * only the name of a attribute will be remaining
148                          */
149                 } else {
150                         cr = NULL;
151                 }
152 #endif
153                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
154
155 #ifdef LDAP_COMP_MATCH
156                 a->ai_cr = NULL;
157 #endif
158                 ad = NULL;
159                 rc = slap_str2ad( attrs[i], &ad, &text );
160
161                 if( rc != LDAP_SUCCESS ) {
162                         fprintf( stderr, "%s: line %d: "
163                                 "index attribute \"%s\" undefined\n",
164                                 fname, lineno, attrs[i] );
165                         return rc;
166                 }
167
168                 if( slap_ad_is_binary( ad ) ) {
169                         fprintf( stderr, "%s: line %d: "
170                                 "index of attribute \"%s\" disallowed\n",
171                                 fname, lineno, attrs[i] );
172                         return LDAP_UNWILLING_TO_PERFORM;
173                 }
174
175                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
176                         ad->ad_type->sat_approx
177                                 && ad->ad_type->sat_approx->smr_indexer
178                                 && ad->ad_type->sat_approx->smr_filter ) )
179                 {
180                         fprintf( stderr, "%s: line %d: "
181                                 "approx index of attribute \"%s\" disallowed\n",
182                                 fname, lineno, attrs[i] );
183                         return LDAP_INAPPROPRIATE_MATCHING;
184                 }
185
186                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
187                         ad->ad_type->sat_equality
188                                 && ad->ad_type->sat_equality->smr_indexer
189                                 && ad->ad_type->sat_equality->smr_filter ) )
190                 {
191                         fprintf( stderr, "%s: line %d: "
192                                 "equality index of attribute \"%s\" disallowed\n",
193                                 fname, lineno, attrs[i] );
194                         return LDAP_INAPPROPRIATE_MATCHING;
195                 }
196
197                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
198                         ad->ad_type->sat_substr
199                                 && ad->ad_type->sat_substr->smr_indexer
200                                 && ad->ad_type->sat_substr->smr_filter ) )
201                 {
202                         fprintf( stderr, "%s: line %d: "
203                                 "substr index of attribute \"%s\" disallowed\n",
204                                 fname, lineno, attrs[i] );
205                         return LDAP_INAPPROPRIATE_MATCHING;
206                 }
207
208                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
209                         ad->ad_cname.bv_val, mask, 0 ); 
210
211                 a->ai_desc = ad;
212
213                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
214                         a->ai_indexmask = 0;
215                         a->ai_newmask = mask;
216                 } else {
217                         a->ai_indexmask = mask;
218                         a->ai_newmask = 0;
219                 }
220
221 #ifdef LDAP_COMP_MATCH
222                 if ( cr ) {
223                         a_cr = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
224                         if ( a_cr ) {
225                                 /*
226                                  * AttrInfo is already in AVL
227                                  * just add the extracted component reference
228                                  * in the AttrInfo
229                                  */
230                                 rc = insert_component_reference( cr, &a_cr->ai_cr );
231                                 if ( rc != LDAP_SUCCESS) {
232                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
233                                         return LDAP_PARAM_ERROR;
234                                 }
235                                 continue;
236                         } else {
237                                 rc = insert_component_reference( cr, &a->ai_cr );
238                                 if ( rc != LDAP_SUCCESS) {
239                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
240                                         return LDAP_PARAM_ERROR;
241                                 }
242                         }
243                 }
244 #endif
245                 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
246                                  ainfo_cmp, avl_dup_error );
247
248                 if( rc ) {
249                         if ( bdb->bi_flags & BDB_IS_OPEN ) {
250                                 AttrInfo *b = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
251                                 /* If we were editing this attr, reset it */
252                                 b->ai_indexmask &= ~BDB_INDEX_DELETING;
253                                 /* If this is leftover from a previous add, commit it */
254                                 if ( b->ai_newmask )
255                                         b->ai_indexmask = b->ai_newmask;
256                                 b->ai_newmask = a->ai_newmask;
257                                 ch_free( a );
258                                 continue;
259                         }
260                         fprintf( stderr, "%s: line %d: duplicate index definition "
261                                 "for attr \"%s\"" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
262                                 fname, lineno, attrs[i] );
263
264                         return LDAP_PARAM_ERROR;
265                 }
266         }
267
268         ldap_charray_free( attrs );
269         if ( indexes != NULL ) ldap_charray_free( indexes );
270
271         return LDAP_SUCCESS;
272 }
273
274 static int
275 bdb_attr_index_unparser( void *v1, void *v2 )
276 {
277         AttrInfo *ai = v1;
278         BerVarray *bva = v2;
279         struct berval bv;
280         char *ptr;
281
282         slap_index2bvlen( ai->ai_indexmask, &bv );
283         if ( bv.bv_len ) {
284                 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
285                 ptr = ch_malloc( bv.bv_len+1 );
286                 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
287                 *bv.bv_val++ = ' ';
288                 slap_index2bv( ai->ai_indexmask, &bv );
289                 bv.bv_val = ptr;
290                 ber_bvarray_add( bva, &bv );
291         }
292         return 0;
293 }
294
295 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
296 static AttrInfo aidef = { &addef };
297
298 void
299 bdb_attr_index_unparse( struct bdb_info *bdb, BerVarray *bva )
300 {
301         if ( bdb->bi_defaultmask ) {
302                 aidef.ai_indexmask = bdb->bi_defaultmask;
303                 bdb_attr_index_unparser( &aidef, bva );
304         }
305         avl_apply( bdb->bi_attrs, bdb_attr_index_unparser, bva, -1, AVL_INORDER );
306 }
307
308 static void
309 bdb_attrinfo_free( void *v )
310 {
311         AttrInfo *ai = v;
312 #ifdef LDAP_COMP_MATCH
313         free( ai->ai_cr );
314 #endif
315         free( ai );
316 }
317
318 void
319 bdb_attr_index_destroy( Avlnode *tree )
320 {
321         avl_free( tree, bdb_attrinfo_free );
322 }
323
324 void bdb_attr_index_free( struct bdb_info *bdb, AttributeDescription *ad )
325 {
326         AttrInfo *ai;
327
328         ai = avl_delete( &bdb->bi_attrs, ad, ainfo_type_cmp );
329         if ( ai )
330                 bdb_attrinfo_free( ai );
331 }
332
333 /* Get a list of AttrInfo's to delete */
334
335 typedef struct Alist {
336         struct Alist *next;
337         AttrInfo *ptr;
338 } Alist;
339
340 static int
341 bdb_attrinfo_flush( void *v1, void *arg )
342 {
343         AttrInfo *ai = v1;
344
345         if ( ai->ai_indexmask & BDB_INDEX_DELETING ) {
346                 Alist **al = arg;
347                 Alist *a = ch_malloc( sizeof( Alist ));
348                 a->ptr = ai;
349                 a->next = *al;
350                 *al = a;
351         }
352         return 0;
353 }
354
355 void bdb_attr_flush( struct bdb_info *bdb )
356 {
357         Alist *al = NULL, *a2;
358
359         avl_apply( bdb->bi_attrs, bdb_attrinfo_flush, &al, -1, AVL_INORDER );
360
361         while (( a2 = al )) {
362                 al = al->next;
363                 avl_delete( &bdb->bi_attrs, a2->ptr, ainfo_cmp );
364                 bdb_attrinfo_free( a2->ptr );
365                 ch_free( a2 );
366         }
367 }