]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attr.c
fix previous commit
[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 }
293
294 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
295 static AttrInfo aidef = { &addef };
296
297 void
298 bdb_attr_index_unparse( struct bdb_info *bdb, BerVarray *bva )
299 {
300         if ( bdb->bi_defaultmask ) {
301                 aidef.ai_indexmask = bdb->bi_defaultmask;
302                 bdb_attr_index_unparser( &aidef, bva );
303         }
304         avl_apply( bdb->bi_attrs, bdb_attr_index_unparser, bva, -1, AVL_INORDER );
305 }
306
307 static void
308 bdb_attrinfo_free( void *v )
309 {
310         AttrInfo *ai = v;
311 #ifdef LDAP_COMP_MATCH
312         free( ai->ai_cr );
313 #endif
314         free( ai );
315 }
316
317 void
318 bdb_attr_index_destroy( Avlnode *tree )
319 {
320         avl_free( tree, bdb_attrinfo_free );
321 }
322
323 void bdb_attr_index_free( struct bdb_info *bdb, AttributeDescription *ad )
324 {
325         AttrInfo *ai;
326
327         ai = avl_delete( &bdb->bi_attrs, ad, ainfo_type_cmp );
328         if ( ai )
329                 bdb_attrinfo_free( ai );
330 }
331
332 /* Get a list of AttrInfo's to delete */
333
334 typedef struct Alist {
335         struct Alist *next;
336         AttrInfo *ptr;
337 } Alist;
338
339 static int
340 bdb_attrinfo_flush( void *v1, void *arg )
341 {
342         AttrInfo *ai = v1;
343
344         if ( ai->ai_indexmask & BDB_INDEX_DELETING ) {
345                 Alist **al = arg;
346                 Alist *a = ch_malloc( sizeof( Alist ));
347                 a->ptr = ai;
348                 a->next = *al;
349                 *al = a;
350         }
351         return 0;
352 }
353
354 void bdb_attr_flush( struct bdb_info *bdb )
355 {
356         Alist *al = NULL, *a2;
357
358         avl_apply( bdb->bi_attrs, bdb_attrinfo_flush, &al, -1, AVL_INORDER );
359
360         while (( a2 = al )) {
361                 al = al->next;
362                 avl_delete( &bdb->bi_attrs, a2->ptr, ainfo_cmp );
363                 bdb_attrinfo_free( a2->ptr );
364                 ch_free( a2 );
365         }
366 }