]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attr.c
70a20ea9c1caecd877e21117168e1c24327e725f
[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
27 /* for the cache of attribute information (which are indexed, etc.) */
28 typedef struct bdb_attrinfo {
29         AttributeDescription *ai_desc; /* attribute description cn;lang-en */
30         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
31 #ifdef LDAP_COMP_MATCH
32         ComponentReference* ai_cr; /*component indexing*/
33 #endif
34 } AttrInfo;
35
36 static int
37 ainfo_type_cmp(
38         const void *v_desc,
39         const void *v_a
40 )
41 {
42         const AttributeDescription *desc = v_desc;
43         const AttrInfo  *a = v_a;
44         return SLAP_PTRCMP(desc, a->ai_desc);
45 }
46
47 static int
48 ainfo_cmp(
49         const void      *v_a,
50         const void      *v_b
51 )
52 {
53         const AttrInfo *a = v_a, *b = v_b;
54         return SLAP_PTRCMP(a->ai_desc, b->ai_desc);
55 }
56
57 #ifdef LDAP_COMP_MATCH
58 void
59 bdb_attr_comp_ref(
60         struct bdb_info *bdb,
61         AttributeDescription *desc,
62         ComponentReference** cr )
63 {
64         AttrInfo        *a;
65
66         a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
67         
68         *cr = a != NULL ? a->ai_cr : 0 ;
69 }
70 void
71 bdb_attr_mask_cr(
72         struct bdb_info *bdb,
73         AttributeDescription *desc,
74         slap_mask_t *indexmask,
75         ComponentReference** cr )
76 {
77         AttrInfo        *a;
78
79         a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
80         if ( a ) {
81                 *indexmask = a->ai_indexmask;
82                 *cr = a->ai_cr;
83         } else {
84                 *indexmask = NULL;
85                 *cr = NULL;
86         }
87 }
88 #endif
89
90 void
91 bdb_attr_mask(
92         struct bdb_info *bdb,
93         AttributeDescription *desc,
94         slap_mask_t *indexmask )
95 {
96         AttrInfo        *a;
97
98         a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
99         
100         *indexmask = a != NULL ? a->ai_indexmask : 0;
101 }
102
103 int
104 bdb_attr_index_config(
105         struct bdb_info *bdb,
106         const char              *fname,
107         int                     lineno,
108         int                     argc,
109         char            **argv )
110 {
111         int rc;
112         int     i;
113         slap_mask_t mask;
114         char **attrs;
115         char **indexes = NULL;
116
117         attrs = ldap_str2charray( argv[0], "," );
118
119         if( attrs == NULL ) {
120                 fprintf( stderr, "%s: line %d: "
121                         "no attributes specified: %s\n",
122                         fname, lineno, argv[0] );
123                 return LDAP_PARAM_ERROR;
124         }
125
126         if ( argc > 1 ) {
127                 indexes = ldap_str2charray( argv[1], "," );
128
129                 if( indexes == NULL ) {
130                         fprintf( stderr, "%s: line %d: "
131                                 "no indexes specified: %s\n",
132                                 fname, lineno, argv[1] );
133                         return LDAP_PARAM_ERROR;
134                 }
135         }
136
137         if( indexes == NULL ) {
138                 mask = bdb->bi_defaultmask;
139
140         } else {
141                 mask = 0;
142
143                 for ( i = 0; indexes[i] != NULL; i++ ) {
144                         slap_mask_t index;
145                         rc = slap_str2index( indexes[i], &index );
146
147                         if( rc != LDAP_SUCCESS ) {
148                                 fprintf( stderr, "%s: line %d: "
149                                         "index type \"%s\" undefined\n",
150                                         fname, lineno, indexes[i] );
151                                 return LDAP_PARAM_ERROR;
152                         }
153
154                         mask |= index;
155                 }
156         }
157
158         if( !mask ) {
159                 fprintf( stderr, "%s: line %d: "
160                         "no indexes selected\n",
161                         fname, lineno );
162                 return LDAP_PARAM_ERROR;
163         }
164
165         for ( i = 0; attrs[i] != NULL; i++ ) {
166                 AttrInfo        *a;
167                 AttributeDescription *ad;
168                 const char *text;
169 #ifdef LDAP_COMP_MATCH
170                 ComponentReference* cr = NULL;
171                 AttrInfo *a_cr = NULL;
172 #endif
173
174                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
175                         bdb->bi_defaultmask |= mask;
176                         continue;
177                 }
178
179 #ifdef LDAP_COMP_MATCH
180                 if ( is_component_reference( attrs[i] ) ) {
181                         rc = extract_component_reference( attrs[i], &cr );
182                         if ( rc != LDAP_SUCCESS ) {
183                                 fprintf( stderr, "%s: line %d: "
184                                         "index component reference\"%s\" undefined\n",
185                                         fname, lineno, attrs[i] );
186                                 return rc;
187                         }
188                         cr->cr_indexmask = mask;
189                         /*
190                          * After extracting a component reference
191                          * only the name of a attribute will be remaining
192                          */
193                 } else {
194                         cr = NULL;
195                 }
196 #endif
197                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
198
199 #ifdef LDAP_COMP_MATCH
200                 a->ai_cr = NULL;
201 #endif
202                 ad = NULL;
203                 rc = slap_str2ad( attrs[i], &ad, &text );
204
205                 if( rc != LDAP_SUCCESS ) {
206                         fprintf( stderr, "%s: line %d: "
207                                 "index attribute \"%s\" undefined\n",
208                                 fname, lineno, attrs[i] );
209                         return rc;
210                 }
211
212                 if( slap_ad_is_binary( ad ) ) {
213                         fprintf( stderr, "%s: line %d: "
214                                 "index of attribute \"%s\" disallowed\n",
215                                 fname, lineno, attrs[i] );
216                         return LDAP_UNWILLING_TO_PERFORM;
217                 }
218
219                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
220                         ad->ad_type->sat_approx
221                                 && ad->ad_type->sat_approx->smr_indexer
222                                 && ad->ad_type->sat_approx->smr_filter ) )
223                 {
224                         fprintf( stderr, "%s: line %d: "
225                                 "approx index of attribute \"%s\" disallowed\n",
226                                 fname, lineno, attrs[i] );
227                         return LDAP_INAPPROPRIATE_MATCHING;
228                 }
229
230                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
231                         ad->ad_type->sat_equality
232                                 && ad->ad_type->sat_equality->smr_indexer
233                                 && ad->ad_type->sat_equality->smr_filter ) )
234                 {
235                         fprintf( stderr, "%s: line %d: "
236                                 "equality index of attribute \"%s\" disallowed\n",
237                                 fname, lineno, attrs[i] );
238                         return LDAP_INAPPROPRIATE_MATCHING;
239                 }
240
241                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
242                         ad->ad_type->sat_substr
243                                 && ad->ad_type->sat_substr->smr_indexer
244                                 && ad->ad_type->sat_substr->smr_filter ) )
245                 {
246                         fprintf( stderr, "%s: line %d: "
247                                 "substr index of attribute \"%s\" disallowed\n",
248                                 fname, lineno, attrs[i] );
249                         return LDAP_INAPPROPRIATE_MATCHING;
250                 }
251
252                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
253                         ad->ad_cname.bv_val, mask, 0 ); 
254
255                 a->ai_desc = ad;
256                 a->ai_indexmask = mask;
257 #ifdef LDAP_COMP_MATCH
258                 if ( cr ) {
259                         a_cr = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
260                         if ( a_cr ) {
261                                 /*
262                                  * AttrInfo is already in AVL
263                                  * just add the extracted component reference
264                                  * in the AttrInfo
265                                  */
266                                 rc = insert_component_reference( cr, &a_cr->ai_cr );
267                                 if ( rc != LDAP_SUCCESS) {
268                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
269                                         return LDAP_PARAM_ERROR;
270                                 }
271                                 continue;
272                         } else {
273                                 rc = insert_component_reference( cr, &a->ai_cr );
274                                 if ( rc != LDAP_SUCCESS) {
275                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
276                                         return LDAP_PARAM_ERROR;
277                                 }
278                         }
279                 }
280 #endif
281                 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
282                                  ainfo_cmp, avl_dup_error );
283
284                 if( rc ) {
285                         fprintf( stderr, "%s: line %d: duplicate index definition "
286                                 "for attr \"%s\" (ignored)\n",
287                                 fname, lineno, attrs[i] );
288
289                         return LDAP_PARAM_ERROR;
290                 }
291         }
292
293         ldap_charray_free( attrs );
294         if ( indexes != NULL ) ldap_charray_free( indexes );
295
296         return LDAP_SUCCESS;
297 }
298
299 void
300 bdb_attr_index_destroy( Avlnode *tree )
301 {
302         avl_free( tree, free );
303 }
304