]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attr.c
An incremental step.
[openldap] / servers / slapd / back-bdb / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-bdb.h"
17
18 #if BDB_CONFIG_INDICES
19
20 /* for the cache of attribute information (which are indexed, etc.) */
21 typedef struct ldbm_attrinfo {
22 #ifdef SLAPD_USE_AD
23         AttributeDescription *ai_desc; /* attribute description cn;lang-en      */
24 #else
25         char *ai_desc;
26 #endif
27         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
28 } AttrInfo;
29
30 static int
31 ainfo_type_cmp(
32 #ifdef SLAPD_USE_AD
33         AttributeDescription *desc,
34 #else
35     char                *desc,
36 #endif
37     AttrInfo    *a
38 )
39 {
40 #ifdef SLAPD_USE_AD
41         return ad_cmp( desc, a->ai_desc );
42 #else
43         return( strcasecmp( desc, a->ai_desc ) );
44 #endif
45 }
46
47 static int
48 ainfo_cmp(
49     AttrInfo    *a,
50     AttrInfo    *b
51 )
52 {
53 #ifdef SLAPD_USE_AD
54         return ad_cmp( a->ai_desc, b->ai_desc );
55 #else
56         return( strcasecmp( a->ai_desc, b->ai_desc ) );
57 #endif
58 }
59
60 #if 0
61 void
62 bdb_attr_mask(
63     struct bdb_info     *bdb,
64 #ifdef SLAPD_USE_AD
65         AttributeDescription *desc,
66 #else
67     const char *desc,
68 #endif
69     slap_mask_t *indexmask )
70 {
71         AttrInfo        *a;
72
73         a = (AttrInfo *) avl_find( bdb->bi_attrs, desc,
74             (AVL_CMP) ainfo_type_cmp );
75         
76         *indexmask = a != NULL ? a->ai_indexmask : 0;
77 }
78 #endif
79
80 int
81 bdb_attr_index_config(
82     struct bdb_info     *bdb,
83     const char          *fname,
84     int                 lineno,
85     int                 argc,
86     char                **argv )
87 {
88         int rc;
89         int     i;
90         slap_mask_t mask;
91         char **attrs;
92         char **indexes = NULL;
93
94         attrs = str2charray( argv[0], "," );
95
96         if( attrs == NULL ) {
97                 fprintf( stderr, "%s: line %d: "
98                         "no attributes specified: %s\n",
99                         fname, lineno, argv[0] );
100                 return LDAP_PARAM_ERROR;
101         }
102
103         if ( argc > 1 ) {
104                 indexes = str2charray( argv[1], "," );
105
106                 if( indexes == NULL ) {
107                         fprintf( stderr, "%s: line %d: "
108                                 "no indexes specified: %s\n",
109                                 fname, lineno, argv[1] );
110                         return LDAP_PARAM_ERROR;
111                 }
112         }
113
114         if( indexes == NULL ) {
115                 mask = bdb->bi_defaultmask;
116
117         } else {
118                 mask = 0;
119
120                 for ( i = 0; indexes[i] != NULL; i++ ) {
121                         slap_mask_t index;
122                         rc = slap_str2index( indexes[i], &index );
123
124                         if( rc != LDAP_SUCCESS ) {
125                                 fprintf( stderr, "%s: line %d: "
126                                         "index type \"%s\" undefined\n",
127                                         fname, lineno, indexes[i] );
128                                 return LDAP_PARAM_ERROR;
129                         }
130
131                         mask |= index;
132                 }
133         }
134
135     if( !mask ) {
136                 fprintf( stderr, "%s: line %d: "
137                         "no indexes selected\n",
138                         fname, lineno );
139                 return LDAP_PARAM_ERROR;
140         }
141
142         for ( i = 0; attrs[i] != NULL; i++ ) {
143                 AttrInfo        *a;
144                 AttributeDescription *ad;
145                 const char *text;
146
147                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
148                         bdb->bi_defaultmask = mask;
149                         continue;
150                 }
151
152                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
153
154                 ad = NULL;
155                 rc = slap_str2ad( attrs[i], &ad, &text );
156
157                 if( rc != LDAP_SUCCESS ) {
158                         fprintf( stderr, "%s: line %d: "
159                                 "index attribute \"%s\" undefined\n",
160                                 fname, lineno, attrs[i] );
161                         return rc;
162                 }
163
164                 if( slap_ad_is_binary( ad ) ) {
165                         fprintf( stderr, "%s: line %d: "
166                                 "index of attribute \"%s\" disallowed\n",
167                                 fname, lineno, attrs[i] );
168                         return LDAP_UNWILLING_TO_PERFORM;
169                 }
170
171                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
172                         ( ad->ad_type->sat_approx
173                                 && ad->ad_type->sat_approx->smr_indexer
174                                 && ad->ad_type->sat_approx->smr_filter )
175                         && ( ad->ad_type->sat_equality
176                                 && ad->ad_type->sat_equality->smr_indexer
177                                 && ad->ad_type->sat_equality->smr_filter ) ) )
178                 {
179                         fprintf( stderr, "%s: line %d: "
180                                 "approx index of attribute \"%s\" disallowed\n",
181                                 fname, lineno, attrs[i] );
182                         return LDAP_INAPPROPRIATE_MATCHING;
183                 }
184
185                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
186                         ad->ad_type->sat_equality
187                                 && ad->ad_type->sat_equality->smr_indexer
188                                 && ad->ad_type->sat_equality->smr_filter ) )
189                 {
190                         fprintf( stderr, "%s: line %d: "
191                                 "equality index of attribute \"%s\" disallowed\n",
192                                 fname, lineno, attrs[i] );
193                         return LDAP_INAPPROPRIATE_MATCHING;
194                 }
195
196                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
197                         ad->ad_type->sat_substr
198                                 && ad->ad_type->sat_substr->smr_indexer
199                                 && ad->ad_type->sat_substr->smr_filter ) )
200                 {
201                         fprintf( stderr, "%s: line %d: "
202                                 "substr index of attribute \"%s\" disallowed\n",
203                                 fname, lineno, attrs[i] );
204                         return LDAP_INAPPROPRIATE_MATCHING;
205                 }
206
207 #ifdef NEW_LOGGING
208                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
209                            "attr_index_config: index %s 0x%04x\n",
210                            ad->ad_cname->bv_val, mask ));
211 #else
212                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04x\n",
213                         ad->ad_cname->bv_val, mask, 0 ); 
214 #endif
215
216
217 #ifdef SLAPD_USE_AD
218                 a->ai_desc = ad;
219 #else
220                 a->ai_desc = ch_strdup( ad->ad_cname->bv_val );
221                 ad_free( ad, 1 );
222 #endif
223
224                 a->ai_indexmask = mask;
225
226                 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
227                         (AVL_CMP) ainfo_cmp, (AVL_DUP) avl_dup_error );
228
229                 if( rc ) {
230                         fprintf( stderr, "%s: line %d: duplicate index definition "
231                                 "for attr \"%s\" (ignored)\n",
232                             fname, lineno, attrs[i] );
233
234                         return LDAP_PARAM_ERROR;
235                 }
236         }
237
238         charray_free( attrs );
239         if ( indexes != NULL ) charray_free( indexes );
240
241         return LDAP_SUCCESS;
242 }
243
244
245 static void
246 ainfo_free( void *attr )
247 {
248         AttrInfo *ai = attr;
249 #ifdef SLAPD_USE_AD
250         ad_free( ai->ai_desc, 1 );
251 #else
252         free( ai->ai_desc );
253 #endif
254         free( ai );
255 }
256
257 void
258 bdb_attr_index_destroy( Avlnode *tree )
259 {
260         avl_free( tree, ainfo_free );
261 }
262
263 #endif