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